ADO : 2nd problem : reading value

Started by
6 comments, last by da_cobra 18 years, 11 months ago
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
Advertisement
is it NULL? i have this bug where it crashes if i try to read a NULL value.. never had the time to fix it, so i just keep my stuff not null [lol]
FTA, my 2D futuristic action MMORPG
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]
moe.ron
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?!?
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
pls, some1 :(
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.
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!!!!

This topic is closed to new replies.

Advertisement