6.11. Resolving Data Conflicts

Problem

You need to effectively resolve data conflicts and prevent overwriting of existing data when attempting to update changes in a DataSet to a database where the underlying data has changed.

Solution

Handle the DBConcurrencyException within the RowUpdated event of the DataAdapter.

The schema of table TBL0611 used in this solution is shown in Table 6-13.

Table 6-13. TBL0611 schema

Column name

Data type

Length

Allow nulls?

Id

int

4

No

Field1

nvarchar

50

Yes

The sample code contains seven event handlers:

Form.Load

Creates a DataSet A containing a single DataTable A, filling the schema and data for the table from TBL0611 from the database using a DataAdapter. The ContinueUpdateOnError property of the DataAdapter is set to true. A CommandBuilder object is created to generate the updating logic for the DataAdapter. The default view of DataTable A is bound to a data grid on the form.

A conflict table is created to store the original row data for a row when a concurrency error is encountered while updating a row from DataTable A back to TBL0611 in the database. A DataAdapter that uses a parameterized SQL SELECT statement to retrieve the original row data is created. The schema for the conflict table is loaded from TBL0611 using the DataAdapter. The default view of the conflict table is bound to a data grid on the form.

A DataSet B and a conflict table for DataTable B row update concurrency errors are created in the same way as described for DataSet ...

Get ADO.NET Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.