2.15. 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 schema of table TBL0215 used in this solution is shown in Table 2-14.

Table 2-14. TBL0215 schema

Column name

Data type

Length

Allow nulls?

UniqueId

int

4

No

NumericField

int

4

No

StringNoNullsField

nvarchar

50

No

ConstrainedNegativeField

int

4

No

The sample code contains two event handlers:

Form.Load

Sets up the sample by creating 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.

Update Button.Click

Sets an error description for the first column of the first row using the SetColumnError( ) method if the user has checked the Test Column Error check box. The Update( ) method of the DataAdapter is called to update changes that the user has made to the data back to the database. After the update, the HasErrors property of the DataSet is tested to determine if the DataSet has any errors. The collection of DataTable objects in the DataSet (in this case there is only one) is iterated again and the HasErrors property for each ...

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.