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.LoadCreates a
DataSetAcontaining a singleDataTableA, filling the schema and data for the table from TBL0611 from the database using aDataAdapter. TheContinueUpdateOnErrorproperty of theDataAdapteris set to true. ACommandBuilderobject is created to generate the updating logic for theDataAdapter. The default view ofDataTableAis 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
DataTableAback to TBL0611 in the database. ADataAdapterthat uses a parameterized SQLSELECTstatement 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
DataSetBand a conflict table forDataTableBrow update concurrency errors are created in the same way as described forDataSet ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access