Export (0) Print
Expand All
2 out of 15 rated this helpful - Rate this topic

How to: Handle Concurrency Errors 

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.

NoteNote

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 (DBConcurrencyException ex)
    {
        string customErrorMessage;
        customErrorMessage = "Concurrency violation\n";
        customErrorMessage += ex.Row[0].ToString();
    
        // Add business logic code to resolve the concurrency violation...
    }
    
    
    try
    {
        northwindDataSetCustomersTableAdapter.Update(northwindDataSet);
    }
    catch (DBConcurrencyException ex)
    {
        String customErrorMessage;
        customErrorMessage = "Concurrency violation\n";
        customErrorMessage += ex.get_Row().get_Item(0).ToString();
    
        // Add business logic code to resolve the concurrency violation...
    }
    
    

See Also

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.