ADO Errors
-
Im using ADO to access an SQL server and everything is working ok except for when an error occurs. Then the description and source properites in the error object is *always* null. The native error number is how it should be. Anyone got a clue what could cause this? (we also access the db with MFC and ODBC and there the description is correct) /Magnus
-
Im using ADO to access an SQL server and everything is working ok except for when an error occurs. Then the description and source properites in the error object is *always* null. The native error number is how it should be. Anyone got a clue what could cause this? (we also access the db with MFC and ODBC and there the description is correct) /Magnus
Send your code snippet so we can have a look at what you are doing wrong
-
Send your code snippet so we can have a look at what you are doing wrong
Here is the codesnippet used to get the error. The _ConnectionPtr passed in is either the connection pointer used to execute the command or gotten from _RecordsetPtr->GetActiveConnection() NativeError contains a valid number as said but all strings are NULL. void EmitError(_ConnectionPtr piCon) { if(piCon == NULL) return; ErrorsPtr ptrErrors = piCon->Errors; if(ptrErrors == NULL) return; long lCount = ptrErrors->Count; ErrorPtr ptrError = NULL; CString sError; for(long n = 0; n < lCount; n++) { ptrError = ptrErrors->GetItem(n); if(!(ptrError == NULL)) { LogEmit("%s\nState: %s, Native: %d, Source: %s\n\n", (LPCTSTR)_bstr_t(ptrError->GetDescription()), (LPCTSTR)_bstr_t(ptrError->GetSQLState()), ptrError->NativeError, (LPCTSTR)_bstr_t(ptrError->GetSource()) ); } } } /Magnus
-
Here is the codesnippet used to get the error. The _ConnectionPtr passed in is either the connection pointer used to execute the command or gotten from _RecordsetPtr->GetActiveConnection() NativeError contains a valid number as said but all strings are NULL. void EmitError(_ConnectionPtr piCon) { if(piCon == NULL) return; ErrorsPtr ptrErrors = piCon->Errors; if(ptrErrors == NULL) return; long lCount = ptrErrors->Count; ErrorPtr ptrError = NULL; CString sError; for(long n = 0; n < lCount; n++) { ptrError = ptrErrors->GetItem(n); if(!(ptrError == NULL)) { LogEmit("%s\nState: %s, Native: %d, Source: %s\n\n", (LPCTSTR)_bstr_t(ptrError->GetDescription()), (LPCTSTR)_bstr_t(ptrError->GetSQLState()), ptrError->NativeError, (LPCTSTR)_bstr_t(ptrError->GetSource()) ); } } } /Magnus
use exception handling e.g.
try
{
HRESULT hr = piCon->....;
if (FAILED(hr))
_com_issue_error(hr);}
catch(_com_error& e)
{
CString sBuff = GetErrorDescription(e);
AfxMessageBox(sBuff);
return;
}
catch(...)
{
AfxMessageBox("Unknown Error Occured");
return;
}CString GetErrorDescription(_com_error& e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
_TCHAR szTemp[1024];CString strInfo ; wsprintf(szTemp, \_T("Message : %s\\n"), e.ErrorMessage()); strInfo = szTemp; wsprintf(szTemp, \_T("Code : 0x%08lx\\n"), e.Error()); strInfo += szTemp; wsprintf(szTemp, \_T("Source : %s\\n"), bstrSource.length() ? (LPCTSTR)bstrSource : \_T("null")); strInfo += szTemp; wsprintf(szTemp, \_T("Description : %s\\n"), bstrDescription.length() ? (LPCTSTR)bstrDescription : \_T("null")); strInfo += szTemp; return strInfo;
}
:cool:
-
use exception handling e.g.
try
{
HRESULT hr = piCon->....;
if (FAILED(hr))
_com_issue_error(hr);}
catch(_com_error& e)
{
CString sBuff = GetErrorDescription(e);
AfxMessageBox(sBuff);
return;
}
catch(...)
{
AfxMessageBox("Unknown Error Occured");
return;
}CString GetErrorDescription(_com_error& e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
_TCHAR szTemp[1024];CString strInfo ; wsprintf(szTemp, \_T("Message : %s\\n"), e.ErrorMessage()); strInfo = szTemp; wsprintf(szTemp, \_T("Code : 0x%08lx\\n"), e.Error()); strInfo += szTemp; wsprintf(szTemp, \_T("Source : %s\\n"), bstrSource.length() ? (LPCTSTR)bstrSource : \_T("null")); strInfo += szTemp; wsprintf(szTemp, \_T("Description : %s\\n"), bstrDescription.length() ? (LPCTSTR)bstrDescription : \_T("null")); strInfo += szTemp; return strInfo;
}
:cool:
I do use exception handling, thats from where a try to get the ado-errors. The problem is not catching the error but that the description field is NULL. I can get the HRESULT, native error,.. but that are not very friendly messages. I dont think the code that tries to dig out the error is to blame here, the problem must lie somewhere else,.......and i dont have a clue where. :( /Magnus