
Handling Concurrency Errors
How you handle errors is dependent upon the specific business rules that govern your application. For this walkthrough, after a concurrency violation is raised, the following strategy to handle the concurrency error will be used as an illustration:
The application will present the user with three versions of the record:
The current record in the database.
The original record loaded into the dataset.
The proposed changes in the dataset.
The user is then able to either overwrite the database with the proposed version or cancel the update and refresh the dataset with the new values from the database.
To enable the handling of concurrency errors
Create a custom error handler.
Display choices to the user.
Process the user's response.
Resend the update, or reset the data in the dataset.
Adding Code To Handle the Concurrency Exception
When you attempt to perform an update and an exception gets raised, you generally want to do something with the information provided by the raised exception.
In this section you will add code that will attempt to update the database, and handle any DBConcurrencyException that might get raised, as well as any other exception.
Note: |
|---|
The
CreateMessage and ProcessDialogResults methods will be added later in this walkthrough.
|
To add error handling for the concurrency error
Add the following code below the Form1_Load method:
Private Sub UpdateDatabase()
Try
Me.CustomersTableAdapter.Update(Me.NorthwindDataSet.Customers)
MsgBox("Update successful")
Catch dbcx As Data.DBConcurrencyException
Dim response As Windows.Forms.DialogResult
response = MessageBox.Show(CreateMessage(CType(dbcx.Row, NorthwindDataSet.CustomersRow)), _
"Concurrency Exception", MessageBoxButtons.YesNo)
ProcessDialogResult(response)
Catch ex As Exception
MsgBox("An error was thrown while attempting to update the database.")
End Try
End Sub
private void UpdateDatabase()
{
try
{
this.customersTableAdapter.Update(this.northwindDataSet.Customers);
MessageBox.Show("Update successful");
}
catch (DBConcurrencyException dbcx)
{
DialogResult response = MessageBox.Show(CreateMessage((NorthwindDataSet.CustomersRow)
(dbcx.Row)), "Concurrency Exception", MessageBoxButtons.YesNo);
ProcessDialogResult(response);
}
catch (Exception ex)
{
MessageBox.Show("An error was thrown while attempting to update the database.");
}
}
Replace the CustomersBindingNavigatorSaveItem_Click method to call the UpdateDatabase method so it looks like the following:
Private Sub CustomersBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomersBindingNavigatorSaveItem.Click
UpdateDatabase()
End Sub
private void customersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
UpdateDatabase();
}
Displaying Choices to the User
The code you just wrote calls the CreateMessage procedure to display error information to the user. For this walkthrough, you will use a message box to display the different versions of the record to the user and allow the user to choose whether to overwrite the record with the changes or cancel the edit. Once the user selects an option (clicks a button) on the message box, the response is passed to the ProcessDialogResult method.
To create the message to display to the user
Processing the User's Response
You will also need code to process the user's response to the message box. The options are either to overwrite the current record in the database with the proposed change or abandon the local changes and refresh the data table with the record currently in the database. If the user chooses yes, the Merge method is called with the preserveChanges argument set to true. This will cause the update attempt to be successful, because the original version of the record now matches the record in the database.
To process the user input from the message box