Handling Errors in Managed Applications (SQL Server Compact)

This topic provides code examples that show how to use the error objects provided by the Data Provider for SQL Server Compact. You can use these objects to capture and display engine errors that occur in SQL Server Compact when executing Engine object methods.

When an engine error occurs, a SqlCeException object is created. This exception object contains the SqlCeErrorCollection object. This 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 Compact returns detailed information about an error as a collection of parameters. When you are building error messages, we recommend that you use a series of nested FOR loops to retrieve each parameter in each SqlCeError object in the collection.

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


// Reference the data provider.
using System.Data.SqlServerCe;

// Start the method to generate a database 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);

Visual Basic

' Reference the data provider by using the Imports directive.
Imports System.Data.SqlServerCe

' Start the method to generate a database engine exception.
Public Sub ShowSqlCeException()
    Dim mySelectQuery As String = "SELECT column1 FROM table1"
    Dim myConnection As New SqlCeConnection("Data Source=nonExistSource.sdf;")
    Dim myCommand As New SqlCeCommand(mySelectQuery, myConnection)


    ' Catch the exception as e and pass it to the ShowErrors routine.
    Catch e As SqlCeException


    End Try
End Sub

' Error handling routine that generates an error message
Public Shared Sub ShowErrors(ByVal e As SqlCeException)
    Dim errorCollection As SqlCeErrorCollection = e.Errors

    Dim bld As New StringBuilder()
    Dim inner As Exception = e.InnerException

    If Not inner Is Nothing Then
        MessageBox.Show(("Inner Exception: " & inner.ToString()))
    End If

    Dim err As SqlCeError

    ' Enumerate each error to a message box.
    For Each err In errorCollection
        bld.Append((ControlChars.Cr & " Error Code: " & err.HResult.ToString("X")))
        bld.Append((ControlChars.Cr & " Message   : " & err.Message))
        bld.Append((ControlChars.Cr & " Minor Err.: " & err.NativeError))
        bld.Append((ControlChars.Cr & " Source    : " & err.Source))

        ' Retrieve the error parameter numbers for each error.
        Dim numPar As Integer
        For Each numPar In err.NumericErrorParameters
            If 0 <> numPar Then
                bld.Append((ControlChars.Cr & " Num. Par. : " & numPar))
            End If
        Next numPar

        ' Retrieve the error parameters for each error.
        Dim errPar As String
        For Each errPar In err.ErrorParameters
            If [String].Empty <> errPar Then
                bld.Append((ControlChars.Cr & " Err. Par. : " & errPar))
            End If
        Next errPar

        bld.Remove(0, bld.Length)
    Next err
End Sub