Sign in to follow this  
da_cobra

ADO : 2nd problem : reading value

Recommended Posts

da_cobra    122
hello, me again currently I'm using Recordbinding to read my fields, like this :

if (adFldOK==m_rsADORecSet.m_ulNaamStatus)
		m_strNaam = m_rsADORecSet.m_szNaam ;
	else
	{
		if (m_rsADORecSet.m_ulNaamStatus!=3)
		{
			strStatus.Format("%d", m_rsADORecSet.m_ulNaamStatus) ;
			MessageBox(NULL, "Problemen met het verkrijgen van het 'Naam'-veld : " + strStatus, "Fout!", MB_OK) ;
			return S_FALSE ;
		}
		else
			m_strNaam = _T("") ;



but I want to convert the code so I can drop the recordbinding stuff. But when I try to change the previous code to this new code, my application crashes :
m_strNaam = (char *) (_bstr_t) m_pRS->Fields->GetItem("Naam")- >Value ;


What could be wrong? ("Naam" is a correct field in my database!) TIA Edit/Delete Message

Share this post


Link to post
Share on other sites
moeron    326
what are you casting as a _bstr_t? (what is the type of Value?)


[EDIT]
Also, as graveyard filla was saying - the FIRST thing you should check, especially before posting on a forum, is if your trying to dereference a NULL pointer. Also, if any of them return HRESULTs, check to see if they FAILED
[/edit]

Share this post


Link to post
Share on other sites
da_cobra    122
I cast a CString.
Do you guys mean that if the field "naam" is NULL? If so, no there is a value in it!
And the first code worked fine and I just replaced it with that single line, so I don't think there is something wrong with the rest of my code?!?

Share this post


Link to post
Share on other sites
da_cobra    122
Been searching the whole morning now, still can't find the problem?

I read somewhere on the forum that I can replace

m_strNaam = (char*) (_bstr_t) m_pRS->Fields->GetItem("Naam")->Value ;

with

m_strNaam = (char *) (_bstr_t) m_pRS->GetCollect("Naam") ;

???

Maybe there is something wrong with my initialize code?


::CoInitialize(NULL) ;

m_strConnection = _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = ..//Database.mdb") ;
m_strCmdText = _T("select * from personen") ;
m_pRS = NULL ;
m_piAdoRecordBinding = NULL ;

m_pRS.CreateInstance(__uuidof(Recordset)) ;

try
{
m_pRS->Open((LPCTSTR)m_strCmdText,
(LPCTSTR)m_strConnection,
adOpenKeyset,
adLockOptimistic,
adCmdUnknown) ;
}
catch(_com_error &e)
{
AfxMessageBox(e.Description()) ;
}

if (SUCCEEDED(m_pRS->QueryInterface(__uuidof(IADORecordBinding), (LPVOID*) &m_piAdoRecordBinding)))
{
m_piAdoRecordBinding->BindToRecordset(&m_rsADORecSet) ;
return S_OK ;
}
else
{
MessageBox(NULL, "Fout bij het aanspreken van de database!", "Fout!", MB_OK) ;
return S_FALSE ;
}




Hope some1 can help me out

Share this post


Link to post
Share on other sites
MaulingMonkey    1728
Quote:
Original post by da_cobra
I cast a CString.
Do you guys mean that if the field "naam" is NULL? If so, no there is a value in it!


No, they're talking about all the pointers you're using. You see all the "->"s in this?

m_pRS->Fields->GetItem("Naam")->Value

Those are each a pointer dereference. If any of these is NULL (which is the same as 0 or zero) your program will crash.

Try this:

assert( m_pRS != 0 );
assert( m_pRS->Fields != 0 );
assert( m_pRS->Fields->GetItem("Naam") != 0 );
m_strNaam = m_pRS->Fields->GetItem("Naam")->Value;

Spend some time learning how your debugger works - it'll help with this kind of problem a lot.

Share this post


Link to post
Share on other sites
da_cobra    122
thx for the reply!!!

you helped me on the way!

it seemed that somewhere in my code I read beyond the End of the File
With the previous code, nothing happened, but with GetCollect(), the application just crashed!

Thx to you I found it!!!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this