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 |
|
4 |
No |
Field1 |
|
50 |
Yes |
The sample code contains seven event handlers:
Form.Load
Creates a
DataSet
A
containing a singleDataTable
A
, filling the schema and data for the table from TBL0611 from the database using aDataAdapter
. TheContinueUpdateOnError
property of theDataAdapter
is set to true. ACommandBuilder
object is created to generate the updating logic for theDataAdapter
. The default view ofDataTable
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. ADataAdapter
that uses a parameterized SQLSELECT
statement to retrieve the original row data is created. The schema for the conflict table is loaded from TBL0611 using theDataAdapter
. The default view of the conflict table is bound to a data grid on the form.A
DataSet
B
and a conflict table forDataTable
B
row update concurrency errors are created in the same way as described forDataSet ...
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.