How to: Handle Concurrency Errors

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

You can catch the DBConcurrencyException object to assist in resolving issues arising from concurrency violations. The DBConcurrencyException object returns the data row that caused the error. For more information, see DBConcurrencyException Members.

The following example attempts to update a data source with the contents of NorthwindDataSet from within a try/catch block, if an error is raised an error message along with the first column of the offending data row is displayed.

Note

The code below is an illustration of one strategy in handling a database update error. The code assumes several things; an existing connection to a database, an existing dataset, as well as the assumption that execution of the update command will raise a concurrency violation. For more information and a complete example, see Walkthrough: Handling a Concurrency Exception.

To resolve a concurrency violation

  1. Execute the command to update the database from within a try/catch block.

  2. If an exception is raised, inspect the catch statement's Row property to determine what caused the violation.

  3. Add code to resolve the error based on your application's business rules.

    The following code uses a CustomersTableAdapter and NorthwindDataSet as examples of the adapter and dataset in your application.

    Try
        CustomersTableAdapter.Update(NorthwindDataSet)
    
    Catch ex As DBConcurrencyException
    
        Dim customErrorMessage As String
        customErrorMessage = "Concurrency violation" & vbCrLf
        customErrorMessage &= CType(ex.Row.Item(0), String)
        MessageBox.Show(customErrorMessage)
    
        ' Add business logic code to resolve the concurrency violation...
    
    End Try
    
    try
    {
        customersTableAdapter.Update(northwindDataSet);
    }
    catch (DBConcurrencyException ex)
    {
        string customErrorMessage;
        customErrorMessage = "Concurrency violation\n";
        customErrorMessage += ex.Row[0].ToString();
    
        // Add business logic code to resolve the concurrency violation...
    }
    

See Also

Concepts

What's New in Data Application Development

Binding Windows Forms Controls to Data in Visual Studio

Binding Controls to Data in Visual Studio

Other Resources

Data Walkthroughs

Connecting to Data in Visual Studio

Preparing Your Application to Receive Data

Fetching Data into Your Application

Editing Data in Your Application

Validating Data

Saving Data