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.


// 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



Comments

  • Require information

    Posted by Legacy on 05/08/2003 12:00am

    Originally posted by: Ali Haider

    I need some information, how to edit a ado recordset.
    I am new one in programming feild.
    Please help me.

    Reply
  • error message when using large database

    Posted by Legacy on 06/25/2002 12:00am

    Originally posted by: Josh

    Having a problem executing a query (under ADO recordset open method).
    Using the error handling code, I get the description message:
    "Not enough space on temporary disk"

    My database is large: 390MB --> consisting mainly of one large table with 90 million+ records.
    The query I execute should return about 15 million records.

    After a few hours of processing my ADO code bombs out and get the above error logged.
    The query works as expected on smaller databases.

    I am confused because I have more than 10 GIG of memory on my hard drive. Is it possible this is all being filled?

    Any insight would be appreciated!

    Reply
  • MFC Programmer's Sourcebook

    Posted by Legacy on 09/06/2001 12:00am

    Originally posted by: G.Ravi Kiran

    How to find a transaction already started using ADO
    

    Reply
  • how to catch the error infomation

    Posted by Legacy on 08/29/2001 12:00am

    Originally posted by: emily

    i use ado to connect a datasource using the following statement:
    m_pConnection.CreateInstance(__uuidof(Connection));
    m_pConnection->Open("DSN=dd","","",-1);
    but when the datasource "dd" is not exist,a fatal error will occur.would you kindly tell me how to avoid it?

    Reply
  • How to ExecuteSQL and commit transactions using ADO

    Posted by Legacy on 06/01/2001 12:00am

    Originally posted by: Sajjad Hussain

    I'm using ADO control. I want to execute SQL statements through this control. I can navigate records, delete, update and add new records. Infact I have to delete a set of records from a table in .mdb under certain condition. For this I have to Execute SQL which I don't know how to execute. How to commit transactions through ADO.

    Reply
  • nice/rough

    Posted by Legacy on 04/08/2001 12:00am

    Originally posted by: shahbaz khan

    Good for some of the ruthless user like us.Thanx.

    Reply
  • I am new in database interfaces using ADO and Visual C++

    Posted by Legacy on 01/26/2001 12:00am

    Originally posted by: Wael Eletlbany

    hello everyone
    Can anyone tell me about a website, book, or tutorial about how to connect a database to a Visual C++ interface using ADO. Given that the database is fully relational with multiple tables done using Access 2000. I am saying multiple table because I've seen an example for a single table databse.
    Thank you
    Wael

    Reply
  • Unhandled Exception when using ADO

    Posted by Legacy on 09/29/1999 12:00am

    Originally posted by: Ovais Khan

    Well i use ADO to convert my code from DAO. Most of the time it works fine but can anyone tell me which exception is raised by the ADO object. like DBException is raised by DAO in MFC. _com_error exception is not raised and i do not get the error description in some cases.

    Ovais

    Reply
  • Unknown error

    Posted by Legacy on 08/11/1999 12:00am

    Originally posted by: Simon Wang

    In my program, I got a com error message said "Unknown error 0x800A0E78". Does anybody know what causes this? and what it means? Thanks!

    Reply
  • Some bugs

    Posted by Legacy on 07/06/1999 12:00am

    Originally posted by: Douglas Huang

    This function is taken from one of the MSDN articals.
    
    However, when there is no error object, the statements
    pErrors->Release();
    pError->Release();
    cause an error.
    Who can tell me why?

    Thanks

    Douglas Huang

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds