Search code examples
c#datasetdataadapter

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


We are using windows formms. In the form we have gridview in which we perform insert a new row and update existing row together. And for the process below is the code,

OracleDataAdapter adapter = new OracleDataAdapter(sql, conn);
OracleCommandBuilder oraCmdBuilder = new  OracleCommandBuilder(adapter);
OracleDataAdapter adap = new OracleDataAdapter();
adap.SelectCommand = new OracleCommand(sql, conn);

adap.InsertCommand = oraCmdBuilder.GetInsertCommand();
adap.InsertCommand.Connection = conn;
adap.UpdateCommand = oraCmdBuilder.GetUpdateCommand();
adap.UpdateCommand.Connection = conn;
adap.DeleteCommand = oraCmdBuilder.GetDeleteCommand();
adap.DeleteCommand.Connection = conn;
adap.UpdateBatchSize = 100;

int i = adap.Update(deltaTable);

Where InsertCommand works fine and throws exception in adap.Update(deltaTable); as below,

Concurrency violation: the UpdateCommand affected 0 of the expected 1 records. we tried to add acceptchanges before update method, but that change was affecting delete . The row was not deleting.

Any suggestions are welcome.

Thanks in advance.

Regards


Solution

  • I faced with this problem and below reasons

    1) In your update query you have controlled data with original data before updating. To solve remove this conditions from query. If you remove condition then it turns to last modified win. The conditions have @original prefix. Query like

    WHERE CustomerID = @Original_CustomerID OR @Original_CustomerID IS NULL AND  customer ID IS NULL
    

    2) Before Update in your code anywhere you have Row.AcceptChanges()