Error Handling with ADO | CodeGuru

Error Handling with ADO

I’m new to ADO and I searched for a piece of code to handle errors, I found the following code snippet in an article and wanted to share it with other ADO developers that might be looking for something similar. The following function (LogAdoErrorImport) can be called from any catch block in your program. // […]

Written By
CodeGuru Staff
CodeGuru Staff
Jun 19, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

I’m new to ADO and I searched for a piece of code to handle errors,
I found the following code snippet in an article and wanted to share
it with other ADO developers that might be looking for something
similar.

The following function (LogAdoErrorImport) can be called from
any catch block in your program.

// Obtain information from the Errors Collection
HRESULT LogAdoErrorImport(_ConnectionPtr pConn)
{
  ErrorsPtr   pErrors;
  ErrorPtr    pError;
  CString     strTmp;
  HRESULT     hr = (HRESULT) 0L;
  long        nCount;

  // Don't have an un-handled exception in the handler that
  // handles exceptions!
  try
  {
     pErrors = pConn->GetErrors();

     nCount = pErrors->GetCount();

     for( long i = 0; (!FAILED(hr)) && (i < nCount); i++ )
     {
        TRACE( "t Dumping ADO Error %d of %d", i+1, nCount );

        hr = pErrors->get_Item((_variant_t)((long)i), &pError );

        _bstr_t bstrSource     ( pError->GetSource()      );
        _bstr_t bstrDescription( pError->GetDescription() );
        _bstr_t bstrHelpFile   ( pError->GetHelpFile()    );
        _bstr_t bstrSQLState   ( pError->GetSQLState()    );

        TRACE( "n Number      = %ld", pError->GetNumber()       );
        TRACE( "n Source      = %s",  (LPCTSTR) bstrSource      );
        TRACE( "n Description = %s",  (LPCTSTR) bstrDescription );
        TRACE( "n HelpFile    = %s",  (LPCTSTR) bstrHelpFile    );
        TRACE( "n HelpContext = %ld", pError->GetHelpContext()  );
        TRACE( "n SQLState    = %s",  (LPCTSTR) bstrSQLState    );
        TRACE( "n HelpContext = %ld", pError->GetHelpContext()  );
        TRACE( "n NativeError = %ld", pError->GetNativeError()  );
     }
  }
  catch( CException *e )
  {
     TRACE( "*** UNABLE TO LOG EXCEPTION ***" );
     e->Delete();
  }
  catch(...)
  {
     TRACE( "*** UNABLE TO LOG EXCEPTION ***" );
  }

  pErrors->Release();
  pError->Release();

  return hr;
}

History

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.