This topic has not yet been rated - Rate this topic

Responding to Database Update Errors

Visual Studio .NET 2003

Errors may arise when attempting to update data. A typical error is that the record in the database has changed since it was read into your dataset. Regardless of what caused the error, you must create logic to handle the reconciliation of the record.

In order to resolve the row that raised the error, your application should locate the data row that caused the error, reconcile the problem, clear the error, and re-attempt the update. For more information, see Locating Rows that Have Errors.

When an update command has been executed, the data adapters RowUpdated event is raised for each record affected. By writing an event handler for this event, you can determine whether the update was successful. You can also set the Status property to determine whether the Update command should try to continue processing additional changes, or abort after an error is raised.

To respond to an update error

  1. Create an event handler for the RowUpdated event. For more information, see Writing Event Handlers.
  2. Check the Status property of the RowUpdated event.
    Note   If there is an error, the value will be ErrorsOccurred.
  3. If you want to control whether the update continues or not, set the Status property to a valid value as defined in the UpdateStatus Enumeration.You can also set the IgnoreUpdateErrors property of the data adapter.
  4. Optionally, set the RowError property of the current row in the event object by assigning a value to it. This information is picked up by the dataset's HasErrors property.

    The following example illustrates how you can test whether a row was successfully updated within the RowUpdated event handler. If errors exist, the RowError property of the offending data row is set to the message of the error which automatically sets the HasErrors property to true. At this point you can handle the error based on the type of error raised as well as the needs of your application. Additionally you can set the Status property to continue processing the update, skip the remaining rows, or skip the current row.

    ' Visual Basic
    Private Sub OleDbDataAdapter1_RowUpdated(ByVal sender As Object, _
    ByVal e As System.Data.OleDb.OleDbRowUpdatedEventArgs) _
    Handles OleDbDataAdapter1.RowUpdated
       If e.Status = UpdateStatus.ErrorsOccurred Then
          e.Row.RowError = e.Errors.Message
          ' Add code to reconcile the error.
       End If
    End Sub
    
    // C#
    private void oleDbDataAdapter1_RowUpdated(object sender, System.Data.OleDb.OleDbRowUpdatedEventArgs e)
    {
       if (e.Status == UpdateStatus.ErrorsOccurred) 
       {
          e.Row.RowError = e.Errors.Message;
          // Add code to reconcile the error.
       }
    }
    

See Also

Dataset Updates in Visual Studio .NET | Locating Rows that Have Errors | Writing Event Handlers | UpdateStatus Enumeration | DataSet.HasErrors Property

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.