.netCoders Contact Us
Search:

Data Error Handling

Classes within the ADO.NET namespace use the standard method of error reporting: exceptions. Many of the exceptions thrown by ADO.NET objects are derived from System.Data.DataException.

In the SqlClient object library, the SqlException class encapsulates error information returned by SQL Server. This object has an Errors property which is a collection of SqlError objects. The following code shows how to loop through and display all SqlErrors contained in a SqlException.

public void DisplaySqlErrors(SqlException myException)
{
    for (int i=0; i < myException.Errors.Count; i++)
    {
     Response.Write("Error: " + myException.Errors[i].ToString() + "<br>");
    }
}