A
Arpan
Consider the following code that adds a table to a DataSet dynamically:
<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"))
dSet.Tables.Add(dTable)
'Define the Primary Key
Dim dColumn() As DataColumn =
{dSet.Tables("Users").Columns("ID")}
dSet.Tables("Users").PrimaryKey = dColumn
End Sub
</script>
The above code works fine but if the column named "ID" is set as the
Primary Key before adding the DataTable to the DataSet i.e. if the 2
lines preceding the "End Sub" line are cut & pasted just above the line
"dSet.Tables.Add(dTable)", then the following error gets generated:
Object reference not set to an instance of an object.
pointing to the line
Dim dColumn() As DataColumn = {dSet.Tables("Users").Columns("ID")}
Which "object" is the above error message referring to? Is it the
DataSet or the DataTable or some other object? Also is the error
generating because I am defining the column "ID" as the Primary Key
before adding the DataTable to the DataSet? If yes, then why should
this cause the error? After all, I am defining the Primary Key after
creating the DataTable i.e. the schema of the DataTable is ready after
which only I am defining the Primary Key, isn't it? So why the error?
Thanks,
Arpan
<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"))
dSet.Tables.Add(dTable)
'Define the Primary Key
Dim dColumn() As DataColumn =
{dSet.Tables("Users").Columns("ID")}
dSet.Tables("Users").PrimaryKey = dColumn
End Sub
</script>
The above code works fine but if the column named "ID" is set as the
Primary Key before adding the DataTable to the DataSet i.e. if the 2
lines preceding the "End Sub" line are cut & pasted just above the line
"dSet.Tables.Add(dTable)", then the following error gets generated:
Object reference not set to an instance of an object.
pointing to the line
Dim dColumn() As DataColumn = {dSet.Tables("Users").Columns("ID")}
Which "object" is the above error message referring to? Is it the
DataSet or the DataTable or some other object? Also is the error
generating because I am defining the column "ID" as the Primary Key
before adding the DataTable to the DataSet? If yes, then why should
this cause the error? After all, I am defining the Primary Key after
creating the DataTable i.e. the schema of the DataTable is ready after
which only I am defining the Primary Key, isn't it? So why the error?
Thanks,
Arpan