473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

DataRow?

This is how I am dynamically adding a table to a DataSet:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
'Create an empty DataSet
Dim dSet As New DataSet

'Create a new table & add columns
Dim dTable As New DataTable("Users")
dTable.Columns.Add("ID", Type.GetType("System.Int32"))
dTable.Columns.Add("First Name", Type.GetType("System.String"))
dTable.Columns.Add("Last Name", Type.GetType("System.String"))
dTable.Columns("ID").AutoIncrement = True

'Add the new table to the DataSet's Tables collection
dSet.Tables.Add(dTable)

'Define a Primary Key on the column named ID
Dim dColumn() As DataColumn =
{dSet.Tables("Users").Columns("ID")}
dSet.Tables("Users").PrimaryKey = dColumn

'Add a row to this table
Dim dRow As DataRow = dTable.NewRow()
dRow("ID") = 1
dRow("First Name") = "Patrick"
dRow("Last Name") = "Patterson"
dTable.Rows.Add(dRow)
End Sub
</script>

The output of the above code is displayed in a DataGrid (the code for
which I haven't reproduced here). What I would like to know is like the
DataSet & the DataTable, DataRow is also a class but if the following
line

Dim dRow As DataRow = dTable.NewRow()

is replaced by

Dim dRow As New DataRow

then VWD generates the following error:

'System.Data.DataRow.Protected Sub New(builder As
System.Data.DataRowBuilder)' is not accessible in this context because
it is 'Protected'.

The "New" keyword is used for creating a new instance of an object
(like New DataSet, New DataTable etc.) which, if I am not mistaken,
means that the "New" subs of the DataSet & DataTable objects aren't
protected but unlike the "New" subs of System.Data.DataSet &
System.Data.DataTable, why is the sub "New" of System.Data.DataRow
protected? Why aren't the "New" subs of System.Data.DataSet &
System.Data.DataTable not protected similarly? Or in other words, why
is the "New" sub of DataRow an exception as far as the scope is
concerned?

Moreover simply by using the keyword "NewRow" in the above code - does
that automatically create a new instance of the DataRow object?

I am not sure whether I have expressed myself lucidly enough!

Thanks,

Arpan

Jul 31 '06 #1
1 2790
If I recall how this works correctly, the first part of your code adds the
columns to the table which define the schema - other items such as foreign
key constraints can also be added.
By adding the columns and other constraints you are placing restrictions on
the type of data that can appear in the row. The table.newRow command creates
a row that possesses the schema of the table, ensuring that the data you
enter matches the structure of the table.

Is this okay? I'm pretty new to asp.net myself.

Chris

"Arpan" wrote:
This is how I am dynamically adding a table to a DataSet:

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
'Create an empty DataSet
Dim dSet As New DataSet

'Create a new table & add columns
Dim dTable As New DataTable("Users")
dTable.Columns.Add("ID", Type.GetType("System.Int32"))
dTable.Columns.Add("First Name", Type.GetType("System.String"))
dTable.Columns.Add("Last Name", Type.GetType("System.String"))
dTable.Columns("ID").AutoIncrement = True

'Add the new table to the DataSet's Tables collection
dSet.Tables.Add(dTable)

'Define a Primary Key on the column named ID
Dim dColumn() As DataColumn =
{dSet.Tables("Users").Columns("ID")}
dSet.Tables("Users").PrimaryKey = dColumn

'Add a row to this table
Dim dRow As DataRow = dTable.NewRow()
dRow("ID") = 1
dRow("First Name") = "Patrick"
dRow("Last Name") = "Patterson"
dTable.Rows.Add(dRow)
End Sub
</script>

The output of the above code is displayed in a DataGrid (the code for
which I haven't reproduced here). What I would like to know is like the
DataSet & the DataTable, DataRow is also a class but if the following
line

Dim dRow As DataRow = dTable.NewRow()

is replaced by

Dim dRow As New DataRow

then VWD generates the following error:

'System.Data.DataRow.Protected Sub New(builder As
System.Data.DataRowBuilder)' is not accessible in this context because
it is 'Protected'.

The "New" keyword is used for creating a new instance of an object
(like New DataSet, New DataTable etc.) which, if I am not mistaken,
means that the "New" subs of the DataSet & DataTable objects aren't
protected but unlike the "New" subs of System.Data.DataSet &
System.Data.DataTable, why is the sub "New" of System.Data.DataRow
protected? Why aren't the "New" subs of System.Data.DataSet &
System.Data.DataTable not protected similarly? Or in other words, why
is the "New" sub of DataRow an exception as far as the scope is
concerned?

Moreover simply by using the keyword "NewRow" in the above code - does
that automatically create a new instance of the DataRow object?

I am not sure whether I have expressed myself lucidly enough!

Thanks,

Arpan

Aug 1 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marcel Sottnik | last post by:
Hello group Maybe this is not the right NG, but this problem seems to me to be more language related than ADO.NET. How can I write the constructor for specialization of DataRow which will...
4
by: Michael Carr | last post by:
I have a function that populates a class with values from a database. I'd like to pass into the function either a SqlDataReader or a DataRow, depending on which mechanism I'm using to retrieve data...
2
by: Steve Amey | last post by:
Hi all I am binding a DataRow to some controls on a form. When I've finished editing the DataRow I click on a toolbar button to save the changes. If the focus is on a particular control, the...
6
by: JIM.H. | last post by:
Hello I have; DataRow dr= dataSet11.myTable.NewRow(); And I am filling the fields of this datarow. Now I need to create a copy of this row as drCopy and change a few fields and add both to...
5
by: samoore33 | last post by:
I use the code below to search through a DataSet: Dim t As DataTable t = result.Tables("State") Dim strExpr As String strExpr = "id = '" & theState.ToString() & "'" Dim foundRows() As DataRow...
10
by: mcbobin | last post by:
Hi, Here's hoping someone can help... I'm using a stored procedure to return a single row of data ie a DataRow e.g. public static DataRow GetManualDailySplits(string prmLocationID, string
5
by: Rainer Queck | last post by:
Hello NG, what would be the best way to locate a DataRrow in a DataGridView? I have by DataTable.Select a bunch of DataRows from a DataTable (which is the data source to the DataGridView). Now...
2
by: Ryan Liu | last post by:
Is DataRow uses DataRow and DataRow much efficient than DataRow? Thanks, ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. Ryan Liu Shanghai Fengpu Software Co. Ltd Shanghai , China
9
by: myotheraccount | last post by:
Hello, Is there a way to convert a DataRow to a StringArray, without looping through all of the items of the DataRow? Basically, I'm trying to get the results of a query and put them into a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.