Hello,
I'm using ADO and when I try to load an empty field with the following code, my application crashes.
How can I fix this "little" problem?Code:m_strName = (char*) (_bstr_t) m_pRS->GetCollect("Name") ;
Thanx in advance for any help
Printable View
Hello,
I'm using ADO and when I try to load an empty field with the following code, my application crashes.
How can I fix this "little" problem?Code:m_strName = (char*) (_bstr_t) m_pRS->GetCollect("Name") ;
Thanx in advance for any help
At first glance I see nothing wrong with your code, nor should you have any problems reading in an empty field, otherwise they'll be a lot of databases around the planet just falling over.. ;) So it must be something else, like an invalid column name.
Have you put your code in try... catch(...) blocks? If you haven't its strongly advised that you do. You should have the following set up:
The last catch will tell you exactly whats happened.Code:try
{
...
}
catch(_com_error &e)
{
// find errors here
}
// and for added security I add another catch to find out exactly whats happened:
catch(...)
{
HRESULT hr = (HRESULT) 0L;
ErrorsPtr pErrors = NULL;
ErrorPtr pError = NULL;
int nErrorCode = -1;
pErrors =m_ptrConnection->GetErrors();
int 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( "\t\t Number = %ld\n", pError->GetNumber() );
TRACE( "\t\t Source = %s\n", (LPCTSTR) bstrSource );
TRACE( "\t\t Description = %s\n", (LPCTSTR) bstrDescription );
TRACE( "\t\t HelpFile = %s\n", (LPCTSTR) bstrHelpFile );
TRACE( "\t\t HelpContext = %ld\n", pError->GetHelpContext() );
TRACE( "\t\t SQLState = %s\n", (LPCTSTR) bstrSQLState );
TRACE( "\t\t HelpContext = %ld\n", pError->GetHelpContext() );
TRACE( "\t\t NativeError = %ld\n", pError->GetNativeError() );
nErrorCode = pError->GetHelpContext();
}
}
I also couldn't help noticing the great lengths you go to convert the string into a CString but I would knock all that on the head and keep it simple :
and then just assign it to your member var if you wish.Code:CString strResults(m_pRS->GetCollect("Name"));
Hope that has helped.
Regards
John
i ran into the same problem and could not find away around it other than populating the field with a character that the app would not use.
the record set ado creates somehow does not like getting queried for a null value.
j
Quote:
Originally Posted by da_cobra
In case of BSTR or _bstr_t check if lenght is 0 place a Empty string or Initialize it with something.otherwise it will give you error
uhm, I'm a bit new to VC++, can you give me some example code pls?
thanx in advance
Your code should have a problem with NULL values when you perform the casting...
You may try something like this:
That's if you want to put into the m_strName a "" when finding a NULL.Code:variant_t vtName = m_pRS->GetCollect("Name") ;
m_strName = (vtName.vt == VT_NULL) ? "" : (char*) (_bstr_t) vtName;
thx, it works great with your code now
every1 thx for the help!
panayotisk
I had the same problem in ADO with CString conversions.
The problem is with the variant access if null.
your code example
works fine now that i have used the .vt instead of the bstrVal that Did Not work.Code:m_strName = (vtName.vt == VT_NULL) ? "" : (char*) (_bstr_t) vtName;
thanksCode:vI.bstrVal != VT_NULL