2

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

  1. Add a new row to the DataGridView
  2. Save to the database using adapter.Update(Table);
  3. add another row
  4. alter the value of any column, on both rows
  5. 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

GuidoG
  • 11,359
  • 6
  • 44
  • 79
  • Can't you see the values in the Original_DamageRegionID Column in your DataGridView? If there are Nulls maybe there's something wrong with your Select Statement ... – PrfctByDsgn May 08 '19 at 12:36
  • @PrfctByDsgn No thats missing the point. There is no such column, this variable is created by the adapter and normally has the value of the key column of that row. I have no influence over it, its been done by the adapter – GuidoG May 08 '19 at 12:38
  • If it's not a column in your Datatable then it can't be used by the Update Statement ... – PrfctByDsgn May 08 '19 at 12:43
  • @PrfctByDsgn I think you should read up about how a Adapter works – GuidoG May 08 '19 at 12:46

0 Answers0