Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

ADO.NET: Handle Errors



In addition to Try/Catch and exceptions, the new ADO.NET data architecture allows you to add error messages to each row of Data in a DataSet. SqlDataAdapters attach error messages to Rows if updates or other actions fail. Furthermore, you can filter for rows in error to present them to the user, or pass them to error handling functions.

Errors persist with the DataSet even when being transferred using XML or XML Web services. You can use the RowError property to set the Error message of a DataRow in a DataSet.

		
    myDataSet.Tables["Customers"].Rows[0].RowError = "An error was added";
    myDataSet.Tables["Customers"].Rows[1].RowError = "This is another error message";
    
C#


Now you can walk the error in a DataTable with the GetErrors() method. You can also test for errors using HasErrors.
		
    if ( myDataSet.Tables["Customers"].HasErrors )
    {
      DataRow[] ErrDataRows = myDataSet.Tables["Customers"].GetErrors();
      Console.WriteLine("DataTable " + myDataSet.Tables["Customers"].TableName + " has " + ErrDataRows.Length.ToString() + " Error(s)!");
    
      for (int i = 0; i <= ErrDataRows.Length -1; i++)
      {
        Console.WriteLine("Row Error for row " + ErrDataRows[i]["CustomerID"].ToString() + " --  Error Msg="  + ErrDataRows[i].RowError);
      }
    }
    else
    {
      Console.WriteLine("=================");
      Console.WriteLine("DataTable " + myDataSet.Tables["Customers"].TableName + " Has no errors");
    }
    
C#


The following example loads a DataSet, sets some errors, and then shows the Errors in the Rows.

VB HandleErrors.aspx
Run Sample View Source





Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.