My setup is simple,
- a DataGridView binded to a BindingSource
- The BindingSource is binded to a DataTable
- The DataTable is populated by a simple
adapter.Fill(table);
Now this works well, but since today I get this error in a certain situation.
Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.
I can reproduce it by the following
- Add a new row to the DataGridView
- Save to the database using
adapter.Update(Table);
- add another row
- alter the value of any column, on both rows
- try to save again using
adapter.Update(Table);
Now the error occurs.
I have catched the actual statement send to Sql Server using the profiler, and the statement is this :
exec sp_executesql
N'UPDATE [tblDamageRegion] SET [Remarks] = @Remarks WHERE (([DamageRegionID] = @Original_DamageRegionID))',
N'@Remarks nvarchar(1),@Original_DamageRegionID int',
@Remarks=N'1',@Original_DamageRegionID=NULL
As you can see, the variable @Original_DamageRegionID
is NULL
That causes no rows to be updated, and thus the error above appears.
My question is, how is it possible that the adapter does not fills this variable ?
I tried table.AcceptChanges();
after each adapter.Update(Table);
but it did not help
I tried BindingSource.EndEdit();
before the adapter.Update(Table);
but it did not help