Object reference not set to an instance of an object?

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
 
S

Scott M.

These are the culprits:

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"))

You don't have any actual DataColumns in existence yet, so how can you add
what you don't have to the Columns collection of dTable?

Should be:

dTable.Columns.Add(New DataColumn("ID",
Type.GetType("System.Int32")))
dTable.Columns.Add(New DataColumn("First Name",
Type.GetType("System.String")))
dTable.Columns.Add(New DataColumn("Last Name",
Type.GetType("System.String")))
 
A

Arpan

Scott, you mean to say that the following lines

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"))

in reality, DO NOT create the columns "ID, "First Name" & "Last Name"
in the DataTable in the first place? But if that's the case, I haven't
used "New DataColumn" anywhere in the code I have cited in post #1 but
the DataGrid does display all the columns & the rows correctly when the
code is executed. How come?

Moreover, if the above 3 lines do not create any columns, when do they
get created in the code shown in post #1? In other words, which line in
the code in post #1 actually creates the 3 columns?

Finally, even if I replace the above 3 lines with the 3 lines you have
shown & set the Primary Key on the column "ID" before adding the
DataTable to the DataSet, then still the "Object reference not set to
an instance of an object" gets thrown. Why?

Thanks,

Regards,

Arpan
 
S

Scott M.

Ok, first of all if you attempt to set your primary key like this:

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

dSet.Tables.Add(dTable)

You'll get the object reference error because you are going into the dataset
and looking for table "Users" before you've added it to the dataset in the
first place.

As for the creation of the columns without instantiating them (as I showed
in my last post), I can't explain why it works for you. The columns must be
getting created by implicit means. Nonetheless, it is better practice to
explicitly create the columns ahead of time :

'Create an empty DataSet

Dim dSet As New DataSet

'Create a new table & add columns

Dim dTable As New DataTable("Users")

Dim idCol As New DataColumn("ID", Type.GetType("System.Int32"))

Dim fNameCol As New DataColumn("First Name", Type.GetType("System.String"))

Dim lNameCol As New DataColumn("Last Name", Type.GetType("System.String"))

dTable.Columns.Add(idCol)

dTable.Columns.Add(fNameCol)

dTable.Columns.Add(lNameCol)

dSet.Tables.Add(dTable)

'Define the Primary Key

Dim dColumn As DataColumn() = {dSet.Tables("Users").Columns("ID")}

dSet.Tables("Users").PrimaryKey = dColumn
 
A

Arpan

Thanks, Scott, for the suggestions. You have indeed made it easier for
me to grasp this topic & the reason behind the error message. Moreover,
I have always preferred coding in the manner you have shown & not club
2 lines into one which can be very well done in ASP.NET. I believe it
makes things easier to understand especially for newbies like me.
The columns must be getting created by implicit means

That was my first impression.

One last question (probably!) on this topic - can the Primary Key be
defined on the "ID" column before adding the DataTable to the DataSet?
If yes, then how?

Thanks once again for your helpful advices.

Regards,

Arpan
 
A

Arpan

Yes, the primary key can be set before the table is added to the dataset

How?

Arpan
 
S

Scott M.

Just think about the objects involved here...The PrimaryKey property is a
property of a DataTable. So, when you want to set that property, you need
to have a DataTable. Now, do you have a DataTable before your DataSet is
all set up, yes, you have dTable. So:

Dim dColumn As DataColumn() = {dTable.Columns("ID")}

dTable.PrimaryKey = dColumn
 
S

Scott M.

Also, there is no rule that says that a DataTable *must* be added to a
DataSet at all, so setting the primary key must be possible before adding
the table to the DataSet, since you may not even want to use a dataset
anyway.
 
A

Arpan

Thank you very very very much, Scott, for all the guidance you have
bestowed on me.

Regards,

Arpan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

DataRow? 1
Dynamic DataSet In A DataGrid 1
Creating A DataSet Programmatically 1
Insert New Record 3
DataRow Delete Method 5
Update DataRow 2
DataRow Collection? 3
DropDownList DataGrid 1

Top