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

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

  • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds