VB.NET•Process Conflicting Updates With a Handler for the RowUpdated Event

Listing 2. Offline editing is prone to optimistic concurrency conflicts. In the event of a conflict, the OnRowUpdated event handler saves the failed edit data in a dsEdits DataSet. After submitting successful updates, if any, and updating the local DataSet with recently updated records, users can select failed updates from a dropdown list, retrieve the current records from the database, then reprocess the changes stored in dsEdits.

AddHandler daCusts.RowUpdated, _
   New SqlRowUpdatedEventHandler(AddressOf _
   OnRowUpdated)

Private Shared Sub OnRowUpdated(ByVal sender As _
   Object, ByVal argEvent As _
   SqlRowUpdatedEventArgs)
   'Test for errors and type of error
   Dim strMsg As String
   Dim strTitle As String
   If argEvent.Status = _
      UpdateStatus.ErrorsOccurred Then
      'Handle the error
      Dim rowError As DataRow = argEvent.Row
      If argEvent.StatementType = _
         StatementType.Update Then
         'Store the CustID for the row in the 
         'array
         aintRowFixIDs(intRowFixIndex) = _
            CInt(rowError.Item(0))
         intRowFixIndex += 1
      End If
      'Add a row to dsEdits.Customers table for 
      'each failed edit
      Dim intCtr As Integer
      Dim drEdit As DataRow = _
         dsEdits.Tables(0).NewRow
      drEdit.BeginEdit()
      For intCtr = 0 To 10
         drEdit.Item(intCtr) = _
            argEvent.Row(intCtr)
      Next
      drEdit.EndEdit()
      dsEdits.Tables(0).Rows.Add(drEdit)
      dsEdits.AcceptChanges()
      argEvent.Status = _
         UpdateStatus.SkipCurrentRow
   Else
      'Remove the processed record(s) for the 
      'CustID from dsEdits
      Dim strCustID As String = _
         argEvent.Row.Item(0).ToString
      drEdits = dsEdits.Tables(0).Select( _
         "CustID = " + strCustID)
      If drEdits.Length > 0 Then
         Dim intCtr As Integer
         For intCtr = 0 To drEdits.Length - 1
            drEdits(intCtr).Delete()
         Next intCtr
         dsEdits.AcceptChanges()
      End If
   End If
End Sub