Export (0) Print
Expand All

Using the SQL Server CE Error Control Objects and Collections with eMbedded Visual C++

SQL Server 2000

Using the SQL Server CE Error Control Objects and Collections with eMbedded Visual C++

In Microsoft® SQL Server™ 2000 Windows® CE Edition (SQL Server CE), Replication, RDA, and Engine object errors can be accessed directly in Microsoft eMbedded Visual C++® by using the SQL Server CE error control objects and collections.

The SSCEErrors collection contains an SSCEError object for each error generated. Each SSCEError object contains an SSCEParams collection. The descriptions of errors can be retrieved from SSCEParam objects in the SSCEParams collection. Unlike SQL Server, SQL Server CE returns detailed information about an error as a collection of six parameters. When building error messages, use a series of nested FOR loops to retrieve each SSCEParam object in the SSCEParams collection for each SSCEError object.

Microsoft eMbedded Visual C++ programs reference the SQL Server error objects and collections by adding Ca_mergex20.h and Ca_mergex20.lib to the project references and by referencing these files using the include directive. For more information, see Programmer's Reference for SQL Server CE Error Control Objects and Collections.

Examples

The following example shows how to display Replication, RDA, and Engine object errors using eMbedded VC++.

// Error handling example in eMbedded VC++
#include     "ca_mergex20.h"

void    ShowErrors(ISSCEErrors* pISSCEErrors)
{
HRESULT       hr;
LONG          cbBuf;
LONG          i;
LONG          lErrorCount;
LONG          lErrorIndex;
LONG          lParamCount;
LONG          lParamIndex;
VARIANT       var;
VARIANT       varParam;
WCHAR         wszBuff[4096];
WCHAR*        pwszBuffPos   = &wszBuff[0];
BSTR          bstr;
ISSCEError*   pISSCEError   = NULL;
ISSCEParams*  pISSCEParams  = NULL;
ISSCEParam*   pISSCEParam   = NULL;
BOOL          fSuccess      = FALSE;

// Initialize the variants.
VariantInit(&var);
VariantInit(&varParam);

// Get the count of errors.
if(FAILED(hr = pISSCEErrors->get_Count(&lErrorCount))) goto Exit;
if (lErrorCount <= 0)
    {
    MessageBox(NULL, L"No extended error information.",L"ShowErrors", MB_OK);
    fSuccess = TRUE;
    goto Exit;
    }

// Display errors, one at a time in a single message box.
// If there are too many errors, they may not all display properly.
// If so, you should perform logic based on the number of errors.
for (lErrorIndex = 0; lErrorIndex < lErrorCount; lErrorIndex++)
    {
    cbBuf = swprintf(pwszBuffPos, L"E R R O R  %d of %d\r\n",
        lErrorIndex+1, lErrorCount);
    pwszBuffPos += cbBuf;

    // Get the next error record.
    var.vt = VT_I4;
    var.lVal = lErrorIndex;
    if(FAILED(hr = pISSCEErrors->get_Item(var, &pISSCEError))) goto Exit;

    // Error description
    if (FAILED(hr = pISSCEError->get_Description(&bstr))) goto Exit;
    cbBuf = swprintf(pwszBuffPos, L"DESCRIPTION: '%s'\r\n", bstr);
    pwszBuffPos += cbBuf;
    SysFreeString(bstr);

    // Error number
    if (FAILED(hr = pISSCEError->get_Number(&i))) goto Exit;
    cbBuf = swprintf(pwszBuffPos, L"NUMBER: %8.8X\r\n", i);
    pwszBuffPos += cbBuf;

    // Native error
    if (FAILED(hr = pISSCEError->get_NativeError(&i))) goto Exit;
    cbBuf = swprintf(pwszBuffPos, L"NATIVE_ERROR: %d\r\n", i);
    pwszBuffPos += cbBuf;

    // Error source
    if (FAILED(hr = pISSCEError->get_Source(&bstr))) goto Exit;
    cbBuf = swprintf(pwszBuffPos, L"SOURCE: '%s'\r\n", bstr);
    pwszBuffPos += cbBuf;
    SysFreeString(bstr);

    // Retrieve the error parameters.
    if (FAILED(hr = pISSCEError->get_Params(&pISSCEParams))) goto Exit;

    // Get the number of error parameters.
    if (FAILED(hr = pISSCEParams->get_Count(&lParamCount))) goto Exit;

    // Display the value of each parameter.
    for (lParamIndex = 0; lParamIndex < lParamCount; lParamIndex++)
        {

        // Get the parameter object.
        var.vt = VT_I4;
        var.lVal = lParamIndex;
        if (FAILED(hr = pISSCEParams->get_Item(var, &pISSCEParam))) goto Exit;

        // Get and display the parameter value.
        if (FAILED(hr = pISSCEParam->get_Param(&varParam))) goto Exit;
        if (VT_I4 == varParam.vt || VT_UI4 == varParam.vt)
            {
            cbBuf = swprintf(pwszBuffPos, L"P%d: %d\r\n", lParamIndex,
                (LONG) varParam.lVal);
            }
        else if (VT_I2 == varParam.vt || VT_UI2 == varParam.vt)
            {
            cbBuf = swprintf(pwszBuffPos, L"P%d: %d\r\n", lParamIndex,
                (LONG) varParam.iVal);
            }
        else if (VT_BSTR == varParam.vt)
            {
            cbBuf = swprintf(pwszBuffPos, L"P%d: '%s'\r\n", lParamIndex, 
                varParam.bstrVal);
            }
        pwszBuffPos += cbBuf;

        // Clear the variant.
        VariantClear(&varParam);

        // Release the parameter object.
        pISSCEParam->Release();
        pISSCEParam = NULL;
        }
    cbBuf = swprintf(pwszBuffPos, L"\r\n");
    pwszBuffPos += cbBuf;

    }

// Display the error information.

MessageBox(NULL, wszBuff,L"Error", MB_OK);
fSuccess = TRUE;

Exit:
// Release the parameter object.
if (pISSCEParam)
    {
    pISSCEParam->Release();
    pISSCEParam = NULL;
    }

// Release the parameters object.
if (pISSCEParams)
    {
    pISSCEParams->Release();
    pISSCEParams = NULL;
    }

// Release the error object.
if (pISSCEError)
    {
    pISSCEError->Release();
    pISSCEError = NULL;
    }

// The Errors object is released in calling routine.
if (!fSuccess)
    {
    MessageBox(NULL, L"Error while processing errors!",L"ShowErrors", MB_OK);
    }
return;
}

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