In my application, I have a control that displays the contents of a database table to the user. This control holds the data to display in a System.Data.DataSet
object. The user is able to modify the data displayed in the control, and this data is then committed back into the database when the user is done.
Problems occur when the data in the database table is modified by some external process (e.g. some rows have been updated) while the user is making edits in the control. Ignoring the issue of data correctness for the moment, what I would like to do is to commit the changes the user has made in the control and overwrite the changes made by this external process.
I'm using a SqlDataAdapter
to update the database. In the described use cases, when the underlying database table has not been modified by an external process, SqlDataAdapter.Update
works as expected. However, in the scenario where some external process has fiddled with the table while the user was editing it then SqlDataAdapter.Update
does not throw an exception but returns 0 indicating that no rows were updated. I've checked that rows in my dataset are have the correct data and RowState
(i.e. DataRowState.Modified
) so I know that the data I'm passing in to the SqlDataAdapter.Update
method is correct.
I suppose there are two parts to my question.
- Why is
SqlDataAdapter.Update
not updating the database with the specified dataset? - Why is it silently failing?
I have read this blog entry, and my code does not call AcceptChanges
anywhere, and as I've stated above I have checked the DataSet
's RowState
so I know that the rows are correctly marked as having modified data.