Concurrency violation: the UpdateCommand affected 0 of the expected 1 records. error datagridview?

0

I was developing an application in windows form but when wanting to update the datagridview and save it in the database this error occurs:

  

Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.

all within the method private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)

SqlCommand sqlcmm = new SqlCommand($"SELECT * FROM Cuenta_{todayDate}", connection);
SqlDataAdapter sqlAdapt = new SqlDataAdapter(sqlcmm);
SqlCommandBuilder sqlcmmBuilder = new SqlCommandBuilder(sqlAdapt);
sqlAdapt.Update((DataTable)dataGridView1.DataSource);

Just when I'm editing manually this error appears when editing a cell without a specific pattern after a moment of editing. Any ideas?

    
asked by zeta12900 17.02.2018 в 02:17
source

1 answer

1

What is happening in this case, is that the data in the DB were modified after you made the original query.

The problem is that the update uses an optimistic concurrency to search the data, then write a query in the form

UPDATE (A,B) SET A = valorA, B = valorB WHERE A=ValorAanterior and B=ValorBAnterior

Where:

ValueX is the new value ValueXAnterior is the value that field had before changing it.

Then you are not exactly finding that row to do the update.

To solve this, you will have to revise the structure of your program better and see who else may be affecting that column.

As a side note, if your table is Cuenta_{todayDate} and that todayDate means that you make a table per day, then it means that your database is very poorly designed.

    
answered by 19.02.2018 / 14:53
source