-
July 26th, 2005, 03:17 PM
#1
ADO : error reading an empty field
Hello,
I'm using ADO and when I try to load an empty field with the following code, my application crashes.
Code:
m_strName = (char*) (_bstr_t) m_pRS->GetCollect("Name") ;
How can I fix this "little" problem?
Thanx in advance for any help
-
July 26th, 2005, 03:49 PM
#2
Re: ADO : error reading an empty field
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:
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();
}
}
The last catch will tell you exactly whats happened.
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 :
Code:
CString strResults(m_pRS->GetCollect("Name"));
and then just assign it to your member var if you wish.
Hope that has helped.
Regards
John
Last edited by Vaderman; July 26th, 2005 at 04:08 PM.
Reason: Necessary conversion??
I don't mind that you think slowly but I do mind that you are publishing faster than you think. Wolfgang Pauli, physicist, Nobel laureate (1900-1958)
-
July 26th, 2005, 09:26 PM
#3
Re: ADO : error reading an empty field
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
-
July 27th, 2005, 02:14 AM
#4
Re: ADO : error reading an empty field
Originally Posted by da_cobra
Hello,
I'm using ADO and when I try to load an empty field with the following code, my application crashes.
Code:
m_strName = (char*) (_bstr_t) m_pRS->GetCollect("Name") ;
How can I fix this "little" problem?
Thanx in advance for any help
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
-
July 27th, 2005, 06:21 AM
#5
Re: ADO : error reading an empty field
uhm, I'm a bit new to VC++, can you give me some example code pls?
thanx in advance
-
July 27th, 2005, 06:52 AM
#6
Re: ADO : error reading an empty field
Your code should have a problem with NULL values when you perform the casting...
You may try something like this:
Code:
variant_t vtName = m_pRS->GetCollect("Name") ;
m_strName = (vtName.vt == VT_NULL) ? "" : (char*) (_bstr_t) vtName;
That's if you want to put into the m_strName a "" when finding a NULL.
Extreme situations require extreme measures
-
July 27th, 2005, 08:44 AM
#7
Re: ADO : error reading an empty field
thx, it works great with your code now
every1 thx for the help!
-
July 27th, 2005, 09:18 AM
#8
Re: ADO : error reading an empty field
panayotisk
I had the same problem in ADO with CString conversions.
The problem is with the variant access if null.
your code example
Code:
m_strName = (vtName.vt == VT_NULL) ? "" : (char*) (_bstr_t) vtName;
works fine now that i have used the .vt instead of the bstrVal that Did Not work.
Code:
vI.bstrVal != VT_NULL
thanks
"trampling out the vintage"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|