How to properly commit bindingSource changes to source DB?

kseen picture kseen · Jan 7, 2012 · Viewed 12.9k times · Source

I setup DataGridView and other UI components to allow user to edit data from SQLite DB. But these changes (even they are properly shows in application) doesn't saves to DB. I have tried this code

aBindingSource.EndEdit();
dbDataSetA.GetChanges();
aTableAdapter.Update(dbDataSetA.Accounts);   

but there are concurrency exception:

System.Data.DBConcurrencyException was unhandled Message=Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.

So how should I commit bindingsource changes to DB, guys?


POST EDIT I got this exception when I start the program, then click on second row in DataGridView and then on third and this time program raise a concurrency exception. Hope his help get this issue more detailed.

Thanks in advance, guys!

Answer

watbywbarif picture watbywbarif · Jan 13, 2012

Something like this?

try
{
   aBindingSource.EndEdit();
   dbDataSetA.GetChanges();
   aTableAdapter.Update(dbDataSetA.Accounts);   
}
catch (DBConcurrencyException exc)
{
   MessageBox.Show("original data changed, please redo updates");
   aTableAdapter.Fill(dbDataSetA);
}

Then reassign dbDataSetA as DataSource if needed and user has to enter data again.