Concurrency violation: the UpdateCommand affected 0 of the expected 1 records. DB concurrencyException was unhandled

Alex picture Alex · Nov 29, 2010 · Viewed 18.5k times · Source

i have defined 2 datasets and 2 dataAdapters( one for each of the datasets ) . after creating, for each of the 2 dataAdapters i define a SqlCommandBuilder. All is well till here. I can add, modify, erase very ok from the database using dataAdapter1.Update(dataSet1) .. BUT not in this order :erase, add,modify.

Here is the code for the first dataset,dataAdapter and sqlCommandBuilder :

                string sql = "SELECT * From localitati";
                da1 = new System.Data.SqlClient.SqlDataAdapter(sql, con);
                da1.Fill(ds1, "localitati");
                cmdBuilder1 = new SqlCommandBuilder(da1);

And the second :

            sql = "SELECT * From sucursale";
            da2 = new System.Data.SqlClient.SqlDataAdapter(sql, con);
            da2.Fill(ds2, "sucursale");
            //face automat select, insert ,etc !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            cmdBuilder2 = new SqlCommandBuilder(da2);

Any ideas why is this happening ?

Answer

Spence picture Spence · Nov 29, 2010

Your information supplied is useless. But I can explain the meaning of the error.

Every update command written in ADO.Net is of the form:

Update col1, col2 where col1=col1value AND col2=col2value

ADO.Net keeps the value of the column when it was selected from the database. When it performs the update the condition is that none of the columns have changed when you commit.

The reason you see the error is because the database row has changed in between you performing the select and calling da2.UpdateChanges(ds2). If you look at the logic perhaps you have selected the value for the row into two separate datasets (or in two different threads) and performed an update to it after performing the select.