Handling errors in MFC when using ADO and MS Datagrid ActiveX control

Hello,

I'm writing an app using VS.NET 2003, MFC only (no .NET components), Unicode, Windows XP SP2. I link to ADO using these lines

#import "C:\Program Files\Common Files\System\ADO\msado15.dll" rename( "EOF", "ADOEOF")
#pragma warning(default: 4146)
#import "C:\Program Files\Common Files\System\ole db\oledb32.dll" rename_namespace("msdasc")
#pragma warning(default: 4192)  

Next, I used the form layout editor to add the MS Datagrid ActiveX control. I connect to a table called Workers that contains two fields: ID and Name, both must be non-null and unique. The grid displays this table that's opened in a ADO Recordset object that connects to an Access XP database file.  When the user wants to add a new worker, I ask for the new name and then call the following code

  m_Datagrid.put_Enabled( FALSE);
  try {
     if (!(long)dbRecordset || !(dbRecordset->GetState( ) & ADODB::adStateOpen)) {
        BolekVrany::DisplayError( IDS_ERR_CONNECT_TABLE);
        return;
     }
     dbRecordset->AddNew( );
     _bstr_t    bstr;
     _variant_t var;

     bstr = sName;
     var.bstrVal = bstr;
     var.vt      = VT_BSTR;
     dbRecordset->Fields->GetItem(L"Name")->PutValue( var);
     
     dbRecordset->Update( );
  } catch (_com_error &e) {
     int i = 0;
     try {
         i = 1;
         dbRecordset->CancelUpdate( );
     } catch (_com_error &e) {
         MessageBox( e.Description( ));
         i = 2;
     }
     MessageBox( L"Bla");
  }
  m_Datagrid.put_Enabled( TRUE);

The problem is that when the user enters a duplicate name, the Update method raises an error. It gets caught in the first catch block but when I call CancelUpdate, the Datagrid displays an error message and CancelUpdate raises another error which gets caught by the second handler etc. The problem is that this seems to be recursive so when I catch this exception and call CancelUpdate there, I simply end up with another exception etc. When the grid is not connected to the recordset, the code works fine and the first CancelUpdate works without any exceptions. When grid is connected to the recordset, it fails in the described manned even thought the grid is disabled.

I tried also tried to override the error handler of the grid but it had no effect on the recursive generation of exceptions. I was able to prevent the grid from displaying the message, but the problem remained recursive.

I will be glad for any suggestions on how to handle this except for the most obvious one, ie. disconnecting the grid before trying to update the recordset. The table is short right now but I would like a more elegant solution.

Thanks a lot
bovlkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DanRollinsCommented:
It seems to me that if the Update method fails, there would be no need to use CancelUpdate; that is, since the Update didn't occur, what is there to cancel?

My standard advice is to remove all try/catch handling and allow the error to occur "naturally."   Then you can see the situation stripped of any chance that your error-handling logic might, itself, be responsible for (part of) the problem.

-- Dan
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.