Null exception when updating a DataSet

B

Benton

Hi there,

I am getting a null exception when updating a dataset with my own update
command. In the table, the "ID" field is the Primary Key.

Here's the code:

// Select Command preparation
FbCommand selCmd = new FbCommand("SELECT * FROM SAADUANA", conn);

DataSet ds = new DataSet();
FbDataAdapter da = new FbDataAdapter(selCmd);

da.Fill(ds);

// Update Command preparation
string text = "UPDATE SAADUANA SET DESCRIPTION=@DESCRIPTION WHERE ID=@ID";
FbCommand updCmd = new FbCommand(text, conn);
updCmd.Parameters.Add("@ID", FbDbType.SmallInt, 0);
updCmd.Parameters.Add("@DESCRIPTION", FbDbType.VarChar, 150);
da.UpdateCommand = updCmd;

// Data is changed
DataRow firstRow = ds.Tables[0].Rows[0];
firstRow["DESCRIPTION"] = "ANYTHING";

da.Update(ds); <-- Exception here

System.NullReferenceException was unhandled
"Object reference not set to an instance of an object."

Am I missing something? I appreciate any help or comments.

Best Regards,

-Benton
 
B

Benton

I am getting a null exception when updating a dataset with my own update
command. In the table, the "ID" field is the Primary Key.

I found it. I need to pass the "SourceColumn" argument to the parameter.

Thanks,

-Benton


Here's the code:

// Select Command preparation
FbCommand selCmd = new FbCommand("SELECT * FROM SAADUANA", conn);

DataSet ds = new DataSet();
FbDataAdapter da = new FbDataAdapter(selCmd);

da.Fill(ds);

// Update Command preparation
string text = "UPDATE SAADUANA SET DESCRIPTION=@DESCRIPTION WHERE ID=@ID";
FbCommand updCmd = new FbCommand(text, conn);
updCmd.Parameters.Add("@ID", FbDbType.SmallInt, 0);
updCmd.Parameters.Add("@DESCRIPTION", FbDbType.VarChar, 150);
da.UpdateCommand = updCmd;

// Data is changed
DataRow firstRow = ds.Tables[0].Rows[0];
firstRow["DESCRIPTION"] = "ANYTHING";

da.Update(ds); <-- Exception here

System.NullReferenceException was unhandled
"Object reference not set to an instance of an object."

Am I missing something? I appreciate any help or comments.

Best Regards,

-Benton
 
I

Isaac

Hi,

I assume where you have written "data is changed" below, that bit of of
code is fired on an event of some sort, and is not just run straight
after you create the dataset?

Are you using an ASP .NET front end or a rich client front end? If it's
a ASP .NET solution, make sure that you are correctly persisting the
dataset for round trips.

Also, although you have created your parameters in the parameter
collection correctly, you haven't tied up the parameters with the values
in the dataset. There's a property on the parameter object - I can't
remember the name but it's something like "Source". This should be set
to the field which will be used to populate the parameter value. But
that's not causing your object not set to an instance exception....
either your adapter or your dataset is a null reference - shove a
breakpoint on it and see which one.
 
B

Benton

Also, although you have created your parameters in the parameter
collection correctly, you haven't tied up the parameters with the values
in the dataset. There's a property on the parameter object - I can't
remember the name but it's something like "Source". This should be set to
the field which will be used to populate the parameter value.

Right on the spot. The argument is called "SourceColumn".
But that's not causing your object not set to an instance exception....

Well, adding this "SourceColumn" to the parameter actually fixed my problem,
and the null exception went away. Go figure. :)

Thanks a lot,

-Benton
either your adapter or your dataset is a null reference - shove a
breakpoint on it and see which one.
Hi there,

I am getting a null exception when updating a dataset with my own update
command. In the table, the "ID" field is the Primary Key.

Here's the code:

// Select Command preparation
FbCommand selCmd = new FbCommand("SELECT * FROM SAADUANA", conn);

DataSet ds = new DataSet();
FbDataAdapter da = new FbDataAdapter(selCmd);

da.Fill(ds);

// Update Command preparation
string text = "UPDATE SAADUANA SET DESCRIPTION=@DESCRIPTION WHERE
ID=@ID";
FbCommand updCmd = new FbCommand(text, conn);
updCmd.Parameters.Add("@ID", FbDbType.SmallInt, 0);
updCmd.Parameters.Add("@DESCRIPTION", FbDbType.VarChar, 150);
da.UpdateCommand = updCmd;

// Data is changed
DataRow firstRow = ds.Tables[0].Rows[0];
firstRow["DESCRIPTION"] = "ANYTHING";

da.Update(ds); <-- Exception here

System.NullReferenceException was unhandled
"Object reference not set to an instance of an object."

Am I missing something? I appreciate any help or comments.

Best Regards,

-Benton
 

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