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.LoadSets up the sample by creating a table to demonstrate error information retrieval, using a
DataAdapterto fill it with sample data, and adding it to aDataSet. TheContinueUpdateOnErrorproperty of theDataAdapteris set totrueso 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 theDataAdapteris called to update changes that the user has made to the data back to the database. After the update, theHasErrorsproperty of theDataSetis tested to determine if theDataSethas any errors. The collection ofDataTableobjects in theDataSet(in this case there is only one) is iterated again and theHasErrorsproperty for each ...
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