9.8 Using Row Error Information

The HasErrors property returns a Boolean value indicating whether an error is set on any of the columns in the row. This value can determine whether error-handling code needs to be executed or set custom error information based on custom validation rules. Rather than iterating over the entire collection of rows to locate errors, the GetErrors( ) method of the DataTable can return the array of rows containing errors within the table. The HasErrors property of the DataTable indicates whether there are any rows with errors and should be checked to determine whether calling GetErrors( ) is necessary.

Error information for the row can be set for the entire row or for a specific column in the row. The RowError property sets and retrieves an error description that applies to the entire row. The GetColumnError( ) and SetColumnError( ) methods set and get the error description for a particular column specified with either a column name , column ordinal, or a reference to the column object.

Finally, the ClearErrors( ) method clears all error information for the row and for all columns in the row.

The following example demonstrates how to work with DataRow errors:

 DataRow row; // ... code to set up the row if(row.HasErrors) {     String rowErrorText = row.RowError;          foreach(DataColumn colError in row.GetColumnsInError())     {         String colErrorColumnName = colError.ColumnName;         String colErrorText = row.GetColumnError(colError);         // ... processing for column errors     }     // ... processing for row error     // clear the errors, once processed     row.ClearErrors(); } else {     // no errors in the row } 

As mentioned, errors can also be set on columns, as shown in this example:

 DataRow row; // ... code to set up the row // using the column name row.SetColumnError("MyColumn", "Custom error text, based on name."); // using the ordinal row.SetColumnError(1, "Custom error text, based on ordinal."); 


ADO. NET in a Nutshell
ADO.NET in a Nutshell
ISBN: 0596003617
EAN: 2147483647
Year: 2005
Pages: 415

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net