How to show error message 
Author Message
 How to show error message

Hi all,

   I writed a database program with ADO. The statements I
used are below:

   HRESULT hr;
   _ConnectionPtr pConnection;
   _RecordsetPtr pRecordset;
   try
   {
      hr=pConnection.CreateInstrance("ADODB.Connection");
      if (SUCCEEDED(hr))
           pConnection->Open("....",....);
   }
   catch(_com_error e)
   {
      CString eMsg;
      eMsg.Format("Can't connect to database, error is %s\
                   ",e.Description);
      AfxMessageBox(eMsg);
   }
   ....

   When I run this program, It can't connect to database
due to some reasons.I want to use the statements above to
show the error message, but it showed the message with
wrong code and not to showed correctly.
   Can you help me solve the problem.
   Thanks.

                                                 Dj



Mon, 28 Feb 2005 11:29:43 GMT  
 How to show error message

Quote:

>Hi all,

>   I writed a database program with ADO. The statements I
>used are below:

>   HRESULT hr;
>   _ConnectionPtr pConnection;
>   _RecordsetPtr pRecordset;
>   try
>   {
>      hr=pConnection.CreateInstrance("ADODB.Connection");
>      if (SUCCEEDED(hr))
>           pConnection->Open("....",....);
>   }
>   catch(_com_error e)
>   {
>      CString eMsg;
>      eMsg.Format("Can't connect to database, error is %s\
>                   ",e.Description);
>      AfxMessageBox(eMsg);
>   }
>   ....

>   When I run this program, It can't connect to database
>due to some reasons.I want to use the statements above to
>show the error message, but it showed the message with
>wrong code and not to showed correctly.
>   Can you help me solve the problem.

The function _com_error::Description returns a _bstr_t, and you can't
pass it to the ellipsis parameter in CString::Format. Try casting it
to LPCTSTR, which should invoke the appropriate _bstr_t operator
char/wchar_t* to match the CString %s format specifier.

--
Doug Harrison
Microsoft MVP - Visual C++



Mon, 28 Feb 2005 11:41:39 GMT  
 How to show error message
Dj,

The Description in the _com_error object is a method not a property....

this line of code will yield garbage:
/* ********************* */
eMsg.Format("msg text%s",e.Description);

/* ********************* */

For nonUnicode build try this (verbose form) instead:
/* ********************* */
_bstr_t errd(e.Description());
eMsg.Format("Can't connect to database, error msg is:\n%s",(char *)errd);

/* ********************* */

best regards
roy fine


Quote:
> Hi all,

>    I writed a database program with ADO. The statements I
> used are below:

>    HRESULT hr;
>    _ConnectionPtr pConnection;
>    _RecordsetPtr pRecordset;
>    try
>    {
>       hr=pConnection.CreateInstrance("ADODB.Connection");
>       if (SUCCEEDED(hr))
>            pConnection->Open("....",....);
>    }
>    catch(_com_error e)
>    {
>       CString eMsg;
>       eMsg.Format("Can't connect to database, error is %s\
>                    ",e.Description);
>       AfxMessageBox(eMsg);
>    }
>    ....

>    When I run this program, It can't connect to database
> due to some reasons.I want to use the statements above to
> show the error message, but it showed the message with
> wrong code and not to showed correctly.
>    Can you help me solve the problem.
>    Thanks.

>                                                  Dj



Mon, 28 Feb 2005 12:22:23 GMT  
 How to show error message

Quote:
>    catch(_com_error e)
>    {
>       CString eMsg;
>       eMsg.Format("Can't connect to database, error is %s\
>                    ",e.Description);
>       AfxMessageBox(eMsg);
>    }

Personally, my favorite is:

   catch(_com_error e)
   {
      CString eMsg;
      eMsg.Format("Can't connect to database, error is %s\
                   ",static_cast<LPCTSTR>(e.Description()));
      AfxMessageBox(eMsg);
   }

This compiles in unicode and ANSI builds.  Unfortunately, it's also possible
for it to crash, as if _bstr_t contains a NULL BSTR, this returns a NULL for
the cast.

You'll have to add appropriate checks to make sure that there actually is a
description in there.  I'll leave it to you to come up with some ingenious
code for that.

You might also want to consider the case where Description is empty, but
ErrorMessage has useful information.



Tue, 01 Mar 2005 04:21:31 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to show error message correctly

2. cfe: ERROR MESSAGE 8ERROR MESSAGE 103

3. Error:Too many error or message .......

4. Trap CToolBar Show/Hide message

5. Quick question on custom error messages w/ Error() macro

6. show local parameters in ASSERT message...possible..?!?!?

7. RtlFreeHeap debug message shows negative size?

8. Trap CToolBar Show/Hide message

9. ClassWizard does not show all messages

10. screensaver shows popup message

11. Trap CToolBar Show/Hide message

12. ClassWizard does not show all messages

 

 
Powered by phpBB® Forum Software