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() ...
Get ADO.NET 3.5 Cookbook, 2nd Edition 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.