As end users edit values in the table, they may make mistakes. In these cases, we may not want to stop the user while he or she is in the process of editing the data, but rather simply add the error information to the row for display later. The DataRow supports this ability by providing a RowError property on each row.
The two examples below show how to add/read error information associated with a DataRow.
Example 1:
[VB] Public Sub AddErrorToRow(ByVal workTable As DataTable, ByVal Row As Integer, _ ByVal ErrorString As String) workTable.Rows(Row).RowError = ErrorString End Sub [C#] public void AddErrorToRow(DataTable workTable, int Row, string ErrorString) { workTable.Rows[Row].RowError = ErrorString; }
Example 2:
Public Sub WriteTableErrors(ByVal workTable As DataTable)
If workTable.HasErrors Then
Dim ErrorRows() As DataRow = workTable.GetErrors()
Console.WriteLine("")
Console.WriteLine("=================")
Console.WriteLine("DataTable " & workTable.TableName & " has " & _
ErrorRows.Count.ToString & " Errors!")
Dim i As Integer
For i = 0 to Errors.Count – 1
Console.WriteLine("Row Error for row " & _
ErrorRows(i)("CustID").ToString & "Error Msg =" & _
ErrorRows(i).RowError)
Next
Else
Console.WriteLine("")
Console.WriteLine("=================")
Console.WriteLine("DataTable " + workTable.TableName + " Has no errors")
End If
End Sub
[C#]
public void WriteTableErrors(DataTable workTable)
{
if ( workTable.HasErrors )
{
DataRow[] ErrorsRows = workTable.GetErrors();
Console.WriteLine("");
Console.WriteLine("=================");
Console.WriteLine("DataTable " + workTable.TableName + " has " +
ErrorsRows.Count.ToString() + " Errors!");
for (int i = 0; i <= ErrorsRows.Count -1; i++)
{
Console.WriteLine("Row Error for row " +
ErrorsRows[i]["CustID"].ToString() + " Error Msg=" +
ErrorsRows[i].RowError);
}
}
else
{
Console.WriteLine("");
Console.WriteLine("=================");
Console.WriteLine("DataTable " + workTable.TableName + " Has no errors");
}
}
The first example takes a row number (Row) and adds an error message (ErrorString). The second example prints the RowError for each row of the workTable that has an error associated with it.