Extracting Errors From DataTable objects
The DataTable object has a nice feature when it comes to error handling. It has a HasErrors property that determines whether any errors exist in the table object. Using this, it's possible to loop through all the rows, then check the GetColumnsInError method to collect error information with the GetColumnError method, as illustrated below:
public
static string[] GetRowErrorInformation(DataTable table) {
if (!table.HasErrors)
return new string[] { };
List<string> errors = new List<string>();
foreach (DataRow row in table.GetErrors()) {
DataColumn[] columns = row.GetColumnsInError();
foreach (DataColumn column in columns)
errors.Add(row.GetColumnError(column));
}
return errors.ToArray();
}
In this example, the errors are returned as a string array. If you need a single string, you can simply use string.Concat() to concatenate the values.