I’m having some trouble updating Access tables from a DataGridView.
What’s confusing me is that the code beblow works with one table and not with another, but they both have the exact same structure.
The tables
I have two tables with multiple (identical) fields, in which the primary key is a field called “Number” ; this field is Autoincrement, and indexed without duplicates, in both tables.
So I read about the lack of primary key as the origin of concurrency violation, but it doesn’t seem to be the issue here.
The code
Here is the relevant part of my code, gathered together :
Initialization
BindingSource BS = new BindingSource();
DbDataAdapter adapter;
DataTable table = new DataTable();
OdbcCommand command = new OdbcCommand(query, odbcConnection);
OdbcDataAdapter adapter = new OdbcDataAdapter(command);
adapter.AcceptChangesDuringUpdate = true; // Attempt to fix the issue
adapter.AcceptChangesDuringFill = true; // same
DbCommandBuilder commandBuilder = new OdbcCommandBuilder(adapter);
dgv.DataSource = BS;
BS.DataSource = table;
adapter.Fill(table);
Saving the changes
OdbcCommandBuilder builder = new OdbcCommandBuilder((OdbcDataAdapter)adapter);
adapter.UpdateCommand = builder.GetUpdateCommand(); // Fix attempt
adapter.Update(table); // Where the exception is thrown
Considering my problem happens quite randomly depending on the database used, I think it’s database related, but they got the same structure, and both are not in use (copied on my local drive) so I really don’t have a clue what’s happening.
So I changed the corporate class to use OleDb instead of Odbc, worked like a charm !
The code remains exactly the same regarding the
DataGridView, exceptOdbcCommandbecomesOleDbCommandandOdbcDataAdapterbecomesOleDbDataAdapter.