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

  • Question

  • I have searched this forum as well as looked online, so many people have this issue but no one has exact solution. Please check the code below.

    On first save click, If any of the child table's (xref tables in this case) update fails, the transaction roll backs. but then if i click Save again the workOrderTableAdapter.Update() fails with error "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records." If i do quick watch on workOrderTableAdapter.Rows[0].WorkOrderId i can see the workOrderId created on the first click is still there, so dataset was never rolled back. How do i fix this issue.

    private void workOrderBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
          try
          {
            this.Validate();
            this.workOrderBindingSource.EndEdit();
            long newWorkOrderId = -1;
            using (TransactionScope scope = new TransactionScope())
            {
              //Save work order
              this.workOrderTableAdapter.Update(this.myDataSet.WorkOrder);
    
              //Get new work order id
              newWorkOrderId = Convert.ToInt64(this.myDataSet.WorkOrder.Rows[0]["WorkOrderId"]);
    
              //Attach work order id to child tables
              AttachWorkOrderId(newWorkOrderId);
    
              //Save child tables
              this.workOrderEmployeeXrefBindingSource.EndEdit();
              this.workOrderItemXrefBindingSource.EndEdit();
              this.workOrderEmployeeXrefTableAdapter.Update(this.myDataSet.WorkOrderEmployeeXref);
              this.workOrderItemXrefTableAdapter.Update(this.myDataSet.WorkOrderItemXref);
    
              //Accept changes
              this.myDataSet.AcceptChanges();
    
              //Complete the transaction
              scope.Complete();
            }
          }
          catch (Exception ex)
          {
            MessageBox.Show("Error");
          }
        }
    Thursday, December 30, 2010 4:55 PM

Answers

All replies