Question : SqlDataAdapter InsertCommand not updating primary key value in dataset

I've got a form which I'm syncing with a dataset.  My problem is that when I create a new record, which'll get added to the database using the insertcommand that the SqlCommandBuilder built, the dataset doesn't contain the new record's primary key value.  So if I then call Update on the adapter again for this record (which'll then use the updatecommand as the record already exists), I get the following error:

"Concurrency violation: the UpdateCommand affected 0 of the expected 1 records."

This is because the update command's WHERE clause parameter that should contain the primary key value, instead contains NULL.

It works if in my form, I add the new record, then close and reopen the form, then change the fields, then update.  This is because it's rebinding when the form loads.  I just can't insert, then update a record without rebinding the controls.

I'm probably misunderstanding something fundamental here.  Should there be something else I'm calling to set the primary key field in the dataset after a new record gets inserted?

Thankyou for any help with this,
Dan.

Answer : SqlDataAdapter InsertCommand not updating primary key value in dataset

DataAdapter does not include the primary key information implicitly. You need to fill schema before you fill in the data. Please check this link.
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter(VS.71).aspx
http://msdn.microsoft.com/en-us/library/system.data.common.dbdataadapter.fillschema(v=VS.71).aspx
Random Solutions  
 
programming4us programming4us