GetFieldValue() throws an exception
-
I have a code like this CString cSQL; CDatabase db; db.OpenEx( NULL, CDatabase::forceOdbcDialog ); CRecordset tbl( &db ); cSQL.Format( "SELECT * FROM tablename WHERE ID = %d", nID ); tbl.Open( CRecordset::forwardOnly, cSQL ); try { tbl.GetFieldValue( "ID", cSQL ); tbl.GetFieldValue( "ID", cSQL ); } catch( CException* e ) { char szCause[255]; e->GetErrorMessage( szCause, 255 ); e->Delete(); } The first GetFieldValue() call gets appropriate value into cSQL variable. But on the second GetFieldValue() call I get CDBException and the value of szCause is "" - empty string and m_nRetCode member of CException object is 100. What is wrong in this code? Any help would be very helpful. Thanks in advance. Stefan
-
I have a code like this CString cSQL; CDatabase db; db.OpenEx( NULL, CDatabase::forceOdbcDialog ); CRecordset tbl( &db ); cSQL.Format( "SELECT * FROM tablename WHERE ID = %d", nID ); tbl.Open( CRecordset::forwardOnly, cSQL ); try { tbl.GetFieldValue( "ID", cSQL ); tbl.GetFieldValue( "ID", cSQL ); } catch( CException* e ) { char szCause[255]; e->GetErrorMessage( szCause, 255 ); e->Delete(); } The first GetFieldValue() call gets appropriate value into cSQL variable. But on the second GetFieldValue() call I get CDBException and the value of szCause is "" - empty string and m_nRetCode member of CException object is 100. What is wrong in this code? Any help would be very helpful. Thanks in advance. Stefan
The problem is most like caused because you're trying to access the same column's value twice. I haven't debugged into the GetFieldValue code but I do know from experience that you may only retrieve a column's value once and in the proper order. In other words, if you do SELECT *, you must access the columns in the same order as they appear on the table. If you do a SELECT COL1, COL2, ... you must call GetFieldValue for COL1 before calling it for COL2. I hope this helps, Alvaro