Handling Errors in Managed Applications (SQL Server Compact Edition)
This topic provides code examples that show how to use the error objects provided by the Data Provider for SQL Server 2005 Compact Edition (SQL Server Compact Edition). You can use these objects to capture and display engine errors that occur in SQL Server Compact Edition when executing Replication, RemoteDataAccess, or Engine object methods.
The SqlCeException Object
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 Edition 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.
Examples
In the following example, the ShowSqlException method catches a SQL Server Compact Edition 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.
C#
// Reference the .NET Compact Framework Data Provider for SQL Server Compact Edition.
using System.Data.SqlServerCe;
// Start the method to generate a SQL Server Compact Edition 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);
try
{
myCommand.Connection.Open();
}
// Catch the exception as e and pass it to the ShowErrors routine.
catch (SqlCeException e)
{
ShowErrors(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);
}
MessageBox.Show(bld.ToString());
bld.Remove(0, bld.Length);
}
}
Visual Basic
' Reference the .NET Compact Framework Data Provider for SQL Server Compact Edition by using the Imports directive.
Imports System.Data.SqlServerCe
' Start the method to generate a SQL Server Compact Edition 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)
Try
myCommand.Connection.Open()
' Catch the exception as e and pass it to the ShowErrors routine.
Catch e As SqlCeException
ShowErrors(e)
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
MessageBox.Show(bld.ToString())
bld.Remove(0, bld.Length)
Next err
End Sub
See Also
Reference
Managed Data Provider (SQL Server Compact Edition)
Handling Errors in Native Applications
Concepts
Building Smart Device Applications (SQL Server Compact Edition)
Building Application for Desktop and Tablet PCs (SQL Server Compact Edition)