By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,239 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

DataRow?

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.