NullReference Exception on DataTable.Rows.Add

G

Guest

Hi,

I would just like to advice on how to fix a problem I'm having in adding a
new row to a DataTable.

Anyway, my problem is that this code always gives me the dreaded "object
reference not set to an instance" exception:

[C#] (WinForms)

DataRow row;

row = DataSet.Tables[0].NewRow();
// set row column values here
DataSet.Tables[0].Rows.Add(row); //<< exception on this line

I've check on the values for DataSet, Tables[0], Tables[0].Rows and row...
They all have valid values, so how come the exception is thrown?

I've also tried something like:

DataRow row;
DataTable table = DataSet.Tables[0];
row = table.NewRow();
//set column values here
table.Rows.Add(row) //<< exception still gets thrown here

I'm using Visual Studio 2003 with .NET framework sp1 installed on Windows XP
professional with SP2...

The funny thing is that within the same event, but under the code for a
different "case" block, i have a similar set of code for a different table
within the same dataset but the exception doesn't get thrown during the
Rows.Add portion.

One more thing, this wasn't a problem when the .NETFX SP1 wasn't installed
yet.

I would really appreciate any insight anyone can give on this, we really
need to install SP1, because of certain fixes that it introduced.

I'm thinking maybe some of the Post-SP1 "hotfixes" might address this
problem, but I don't know which.

BTW, there's someone else who encountered a similar error, he's already
posted his problem on several forums last October 2004 and there's no reply
to his post, I'm just not sure if he has SP1 installed. Links to his posts
are
http://www.techietwo.com/detail-6334024.html[/URL
[URL]http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic2591.aspx


you can also try searching for "datatable.insertrow object reference" on
google groups, it's one of the 2 posts that will be shown in the search
results.


TIA.

raist[/URL]
 
N

Norman Yuan

Is "DataSet" in your code an instance of DataSet object?

I mean, you must have something like

DataSet DataSet=new DataSet();

prior to you code snippet for adding rows.

I do not even want to try if this code works or not, just want to ask why
you have a variable that is the same as a class name. It is bad practice,
even it works (I doubt).

I would:

DataSet myDataSet=new DataSet()
//Add or create table in it

//Then
DataRow row=myDataSet.Tables[0].NewRow();

So, it is clear "myDataSet" is an instance of DataSet object
 
R

Raist de Jesus

Sorry for the misunderstanding,

I just used the word "DataSet" as an example. In my code it's called
"DataBind". The problem isn't because of a DataSet variable named "DataSet".


Thanks.

raistlin
 
G

Guest

Just an update:

When I change the code from:

DataRow row;
row = dataSet.Tables[0].NewRow();
dataSet.Tables[0].Rows.Add(row); //<< code throws NullReferenceException here

to:

DataRow row;
row = dataSet.Tables[0].NewRow();
try
{
dataSet.Tables[0].Rows.Add(row);
}
catch (NullReferenceException)
{
dataSet.Tables[0].Rows.Add(row);
}

The application behaves "normally", although the NullReferenceException
still occurs.
Is there a way to actually detect "what" is "actually" causing the
NullReferenceException? Because, although it "works", this "fix" still isn't
"right".
Thanks in advance.
 

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

Top