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? |
---|---|---|---|
|
int |
4 |
No |
|
int |
4 |
No |
|
nvarchar |
50 |
No |
|
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 aDataSet
. TheContinueUpdateOnError
property of theDataAdapter
is set totrue
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. TheUpdate( )
method of theDataAdapter
is called to update changes that the user has made to the data back to the database. After the update, theHasErrors
property of theDataSet
is tested to determine if theDataSet
has any errors. The collection ofDataTable
objects in theDataSet
(in this case there is only one) is iterated again and theHasErrors
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.