Handling DataRow and DataColumn Errors (ADO.NET)

To avoid having to respond to row errors while editing values in a DataTable, you can add the error information to the row for later use. The DataRow object provides a RowError property on each row for this purpose. Adding data to the RowError property of a DataRow sets the HasErrors property of the DataRow to true. If the DataRow is part of a DataTable, and DataRow.HasErrors is true, the DataTable.HasErrors property is also true. This applies as well to the DataSet to which the DataTable belongs. When testing for errors, you can check the HasErrors property to determine if error information has been added to any rows.

GetErrors Example

If HasErrors is true, you can use the GetErrors method of the DataTable to return and examine only the rows with errors, as shown in the following example.

Dim workTable As DataTable = New DataTable("Customers")
workTable.Columns.Add("CustID", Type.GetType("System.Int32"))
workTable.Columns.Add("Total", Type.GetType("System.Double"))

AddHandler workTable.RowChanged, New DataRowChangeEventHandler(AddressOf OnRowChanged)

Dim i  As Int32

For i  = 0 To 10
  workTable.Rows.Add(New Object() {i , i *100})
Next

If workTable.HasErrors Then
  Console.WriteLine("Errors in Table " & workTable.TableName)

  Dim myRow As DataRow

  For Each myRow In workTable.GetErrors()
    Console.WriteLine("CustID = " & myRow("CustID").ToString())
    Console.WriteLine(" Error = " & myRow.RowError & vbCrLf)
  Next
End If

Private Shared Sub OnRowChanged( _
    sender As Object, args As DataRowChangeEventArgs)
  ' Check for zero values.
  If CDbl(args.Row("Total")) = 0 Then args.Row.RowError = _
      "Total cannot be 0."
End Sub
DataTable  workTable = new DataTable("Customers");
workTable.Columns.Add("CustID", typeof(Int32));
workTable.Columns.Add("Total", typeof(Double));

workTable.RowChanged += new DataRowChangeEventHandler(OnRowChanged);

for (int i = 0; i < 10; i+)
  workTable.Rows.Add(new Object[] {i, i*100});

if (workTable.HasErrors)
{
  Console.WriteLine("Errors in Table " + workTable.TableName);

  foreach (DataRow myRow in workTable.GetErrors())
  {
    Console.WriteLine("CustID = " + myRow["CustID"]);
    Console.WriteLine(" Error = " + myRow.RowError + "\n");
  }
}

protected static void OnRowChanged(
    Object sender, DataRowChangeEventArgs args)
{
  // Check for zero values.
  if (args.Row["Total"].Equals(0D))
    args.Row.RowError = "Total cannot be 0.";
}

GetColumnsInError Example

The following example uses the HasErrors property to look for errors. If the row has errors, the GetColumnsInError method returns the array of columns with errors that can then be resolved. The ClearErrors method is then called to clear all errors.

Private Sub GetAllErrs(ByVal row As DataRow)
    ' Declare an array variable for DataColumn objects. 
    Dim colArr() As DataColumn 

    ' If the Row has errors, check GetColumnsInError. 
    Dim i As Integer 
    If row.HasErrors Then  
       ' Get the array of columns in error.
       colArr = row.GetColumnsInError()
       For i = 0 to colArr.Length - 1
          ' Insert code to fix errors on each column.
          Console.WriteLine(colArr(i).ColumnName)
       Next i

    ' Clear errors after reconciling.
    row.ClearErrors()
    End If 
End Sub
private void GetAllErrs(DataRow row)
{
    // Declare an array variable for DataColumn objects.
    DataColumn[] colArr; 
    // If the Row has errors, check GetColumnsInError. 
    if(row.HasErrors)
    {
        // Get the array of columns in error.
        colArr = row.GetColumnsInError();
        for(int i = 0; i < colArr.Length; i+)
        {
            // Insert code to fix errors on each column.
            Console.WriteLine(colArr[i].ColumnName);
        }
        // Clear errors after reconciling.
        row.ClearErrors();
    }
}

See Also

Reference

DataColumnCollection

DataRow

DataTable

Other Resources

Manipulating Data in a DataTable (ADO.NET)

ADO.NET Managed Providers and DataSet Developer Center