5.7. Retrieving Update Errors
Problem
You want to access all of the error information available after an update fails.
Solution
Use one of the available properties (such as HasErrors) and methods (such as GetErrors()) to obtain the error information.
The solution uses a table named RetrieveUpdateErrors in the AdoDotNet35Cookbook database. Execute the following T-SQL to create the table:
USE AdoDotNet35Cookbook GO CREATE TABLE Retrieve UpdateErrors( UniqueId int NOT NULL, NumericField int NOT NULL, StringNoNullsField nvarchar(50) NOT NULL, ConstrainedNegativeField int NOT NULL, CONSTRAINT PK_RetrieveUpdateErrors PRIMARY KEY CLUSTERED ( UniqueId ASC ), CONSTRAINT CK_ConstrainedNegativeField CHECK ( ConstrainedNegativeField < 0) )
The schema of table RetrieveUpdateErrors is shown in Table 5-2.
Table 5-2. RetrieveUpdateErrors table schema
Column name | Data type | Length | Allow nulls? |
|---|---|---|---|
| int | 4 | No |
| int | 4 | No |
| nvarchar | 50 | No |
| int | 4 | No |
The solution creates a table to demonstrate error information retrieval, using a DataAdapter to fill it with sample data, and adding it to a DataSet. The ContinueUpdateOnError property of the DataAdapter is set to true so that an exception is not raised if an error is encountered during an update. Finally, the default view of the table is bound to the data grid on the form.
Specifically, the solution creates one good row and two rows with errors. For each, an error description is set for the column in error using the SetColumnError() ...
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