DataRow primary key is not updated although the DataSet's PK is



Hi Guys

I created a DataRow, that automatically sets its primary key. Let us say 3.

I then added the DataRow to the DataSet and then update the DataSet using
GetChanges() and finally Merge().

The primary key in the Microsoft SQL DB is set to 23 and consequently the PK
in the dataset's row is also set to 23.

What confuses me is that my originally created DataRow does still contain
the old PK value and therefore gives me some troubles.

I thought, that a the DataRow is passed as reference object and should
therefore be the same as the row in the dataset.

Is there any possibility to update this datarow without just getting the
last row from the updated dataset.

Appended the code that I use: ( the dataTable contains two columns, the PK
which is indexed and the name)

DataRow dr = myDataSet.MyTable.NewRow();
dr["Name"] = "Chris"; [dr["ID"] is set to 3!!!]
DataSet changedRecords = myDataSet.GetChanges();
myDataSet.AcceptChanges(); [I am not sure if it needs this!]

myDataSet.MyTable.Rows[0]["ID"] is now 23!!!
dr["ID"] is still 3!!!!

Thanks for your help


try setting the UpdatedRowSource property of the SelectCommand (or maybe the
UpdateCommand) of the DataAdapter to have the value of UpdateRowSource.Both,
I think this will force the update in the DataRow

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