Export (0) Print
Expand All

Error Handling in C#

SQL Server 2000

Error Handling in C#

This topic provides a C# example that shows how to use the error objects provided by the Microsoft® .NET Compact Framework Data Provider for SQL Server CE. These objects can be used to capture and display engine errors that occur in Microsoft SQL Server™ 2000 Windows® CE Edition (SQL Server CE) when executing Replication, RDA, or Engine object methods.

When an engine error occurs, a SqlCeException object is created. This exception object contains the SqlCeErrorCollection object, which in turn contains a collection of SqlCeError objects, one for each error in the exception. The SqlCeErrorCollection object can be accessed directly using the SqlCeException.Errors property. Each SqlCeError object contains an array of error parameters that provide detailed information about the error. Unlike SQL Server, SQL Server CE returns detailed information about an error as a collection of parameters. When building error messages, you should use a series of nested FOR loops to retrieve each parameter in each SqlCeError object in the collection.

For more information, see Programmer's Reference for the .NET Framework Data Providers Error Control Objects and Collections.


In the following example, the ShowSqlException method catches a SQL Server CE engine exception error. This SqlCeException object is passed to the ShowErrors method, which displays each of the SSCEError objects in the SqlCeErrorCollection object. This method loops through all of the error parameters for each error.

// Reference the .NET Compact Framework Data Provider for SQL Server CE.
using System.Data.SqlServerCe;

// Begin the method to generate a SQL Server CE engine exception.
public void ShowSqlCeException() 
    string mySelectQuery = "SELECT column1 FROM table1";
    SqlCeConnection myConnection = new SqlCeConnection("Data Source=nonExistSource.sdf;");
    SqlCeCommand myCommand = new SqlCeCommand(mySelectQuery,myConnection);


    // Catch the exception as e and pass it to the ShowErrors routine.
    catch (SqlCeException e) 


// Error handling routine that generates an error message
public static void ShowErrors(SqlCeException e) 
    SqlCeErrorCollection errorCollection = e.Errors;

    StringBuilder bld = new StringBuilder();
    Exception inner = e.InnerException;

    if (null != inner) 
        MessageBox.Show("Inner Exception: " + inner.ToString());
    // Enumerate the errors to a message box.
    foreach (SqlCeError err in errorCollection) 
        bld.Append("\n Error Code: " + err.HResult.ToString("X")); 
        bld.Append("\n Message   : " + err.Message);
        bld.Append("\n Minor Err.: " + err.NativeError);
        bld.Append("\n Source    : " + err.Source);

        // Enumerate each numeric parameter for the error.
        foreach (int numPar in err.NumericErrorParameters) 
            if (0 != numPar) bld.Append("\n Num. Par. : " + numPar);

        // Enumerate each string parameter for the error.
        foreach (string errPar in err.ErrorParameters) 
            if (String.Empty != errPar) bld.Append("\n Err. Par. : " + errPar);

        bld.Remove(0, bld.Length);

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft