CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2003
    Posts
    107

    Angry c++ and database errors

    In my c++ program I connect to the database and execute some SQL queries. Most of my statements execute fine but once in a while I get errors with my insert statements. I am printing out the error using the SQLError like this. unsigned char error[200], szSqlState[91];
    Code:
      long  pfNativeError; 
        short pcbErrorMsg;
        ::SQLError(NULL,hdbc,hstmt,szSqlState,
          &pfNativeError,(unsigned char*)&error[0],199,&pcbErrorMsg);
    
        LogError("Error executing");
        LogError((char*)command);
        LogError((char*)error);
        ::SQLCancel(hstmt) ;
    Most of the times when I get errors I get the error message in my log files stating an invalid column etc. But some times I get blank line for the error. Did anyone have the same problem. Why do I get a blank line. Please help.

  2. #2
    Join Date
    Mar 2004
    Location
    Central Florida
    Posts
    293

    Re: c++ and database errors

    Your post does not really contain adequate information to trouble shoot the problem. That said:

    In my experience, when an error handler provides a blank message, most of the time it is because I am using an error handler that can trap the error but is the wrong one for providing error information. Below is a list of error handlers I have found/created over the years:

    Code:
    #include "Windows.h"
    #include <stdexcept>
    
      try
      {
        diff_db_schema->PutRefActiveConnection(NULL);
      }
      catch(std::exception& e)
      {
        OutputDebugString(e.what());
      }
      catch( CNotSupportedException *e )
      {
        CString err_msg;  
        CRuntimeClass *pClass = e->GetRuntimeClass();
    
        err_msg = "MFC Exception ";
        err_msg += (LPCTSTR) pClass->m_lpszClassName;
        err_msg += " thrown\n";
    
        AfxMessageBox(err_msg, MB_ICONEXCLAMATION);
      }
      catch( CCommonDialog *e )
      {
        CString err_msg;  
        CRuntimeClass *pClass = e->GetRuntimeClass();
    
        err_msg = "MFC Exception ";
        err_msg += (LPCTSTR) pClass->m_lpszClassName;
        err_msg += " thrown\n";
    
        AfxMessageBox(err_msg, MB_ICONEXCLAMATION);
      }
      catch(_com_error &e)
      {
        CString err_msg;
        _variant_t error_num;
    
        error_num = e.Error();
        error_num.ChangeType(VT_BSTR);
    
        err_msg = "Error:\n";
        err_msg += "Code = ";
        err_msg += (CString)error_num.bstrVal;
        err_msg += "\n";
        err_msg += "Code meaning = ", (char*) e.ErrorMessage();
        err_msg += e.ErrorMessage();
        err_msg += "\n";
        err_msg += "Source = ";
        err_msg += (char*) e.Source();
        err_msg += "\n";
        err_msg += "Description = ";
        err_msg += (char*) e.Description();
        err_msg += "\n";
    
        AfxMessageBox(err_msg, MB_ICONEXCLAMATION);
        
        return false;
      }
      catch(CDaoException* e)
      {
        AfxMessageBox(e->m_pErrorInfo->m_strDescription, MB_ICONEXCLAMATION);
        e->Delete();
        return false;
      }
      catch(CFileException e)
      {
    
        TCHAR   szCause[255];
        CString strFormatted;
    
        e.GetErrorMessage(szCause, 255);
        strFormatted += szCause;
        AfxMessageBox(strFormatted);
        return false;
      } 
      catch(CMemoryException e)
      {
    
        TCHAR   szCause[255];
        CString strFormatted;
    
        e.GetErrorMessage(szCause, 255);
        strFormatted += szCause;
        AfxMessageBox(strFormatted);
        return false;
      } 
      catch(CArchiveException e)
      {
    
        TCHAR   szCause[255];
        CString strFormatted;
    
        e.GetErrorMessage(szCause, 255);
        strFormatted += szCause;
        AfxMessageBox(strFormatted);
        return false;
      } 
      catch(CNotSupportedException e)
      {
    
        TCHAR   szCause[255];
        CString strFormatted;
    
        e.GetErrorMessage(szCause, 255);
        strFormatted += szCause;
        AfxMessageBox(strFormatted);
        return false;
      } 
      catch(CResourceException e)
      {
    
        TCHAR   szCause[255];
        CString strFormatted;
    
        e.GetErrorMessage(szCause, 255);
        strFormatted += szCause;
        AfxMessageBox(strFormatted);
        return false;
      } 
      catch(CDBException e)
      {
    
        TCHAR   szCause[255];
        CString strFormatted;
    
        e.GetErrorMessage(szCause, 255);
        strFormatted += szCause;
        AfxMessageBox(strFormatted);
        return false;
      } 
      catch(COleException e)
      {
    
        TCHAR   szCause[255];
        CString strFormatted;
    
        e.GetErrorMessage(szCause, 255);
        strFormatted += szCause;
        AfxMessageBox(strFormatted);
        return false;
      } 
      catch( char *str )
      {
        AfxMessageBox(str, MB_ICONEXCLAMATION);
        return false;
      }
      catch( _variant_t var )
      {
        CString msg;
    
        var.ChangeType(VT_BSTR);
        msg = var.bstrVal;
    
        AfxMessageBox(msg, MB_ICONEXCLAMATION);
        return false;
      }
      catch( COleDispatchException *e )
      {
        CString err_msg;  
    
        err_msg.Format("MFC COleDispatchException #: %d \r\n"
                       "Description: %s "
                       "Source: %s ", 
                       e->m_wCode, 
                       e->m_strDescription, 
                       e->m_strSource);
    
        AfxMessageBox(err_msg, MB_ICONEXCLAMATION);
    
        success = false;
      }
      catch( CException *e )
      {
        CString err_msg;  
        CRuntimeClass *pClass = e->GetRuntimeClass();
    
        err_msg = "MFC Exception ";
        err_msg += (LPCTSTR) pClass->m_lpszClassName;
        err_msg += " thrown\n";
    
        AfxMessageBox(err_msg, MB_ICONEXCLAMATION);
    
        return false;
      }
      catch(...)
      {
          // requires Windows.h & stdlib.h
        DWORD dw = GetLastError(); 
        char szBuf[80];
    
        ltoa(dw, szBuf, 10); 
    
        LPVOID lpMsgBuf;
        FormatMessage( 
            FORMAT_MESSAGE_ALLOCATE_BUFFER | 
            FORMAT_MESSAGE_FROM_SYSTEM | 
            FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            GetLastError(),
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
            (LPTSTR) &lpMsgBuf,
            0,
            NULL);
        // Display the string.
        AfxMessageBox((LPCTSTR)lpMsgBuf, MB_OK | MB_ICONINFORMATION );
        // Free the buffer.
        LocalFree( lpMsgBuf );
    
        CloseHandle(file_handle);
        return false;
      }
    One of these might give you a more meaningful message.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured