-
GetFieldValue Twice
Hello,
I'm encountering an error with CRecordset::GetFieldValue(). It appears if I first get the value by name, and then try to get it by index, the return fails and returns CDBException. Example:
Code:
//res is a CRecordset * that is at the first row//
//get the field by name//
CString sSourceField = "SOMEFIELD";
CString sSourceValue;
res->GetFieldValue(sSourceField,sSourceValue);
//now loop through by index to get all the fields//
long nFields = res->GetODBCFieldCount();
for (long nIndex = 0; nIndex < nFields; nIndex++)
{
CString sValue;
res->GetFieldValue(nIndex,sValue);
//Fails to get "SOMEFIELD"'s value. Throws a CDBException
//but has no description of the error!//
}
I noticed if I try to get other fields by name before the loop as well, they also fail to be extracted later. Any ideas why I cannot get the same field value twice with two different approaches (by name and then by index)? Does the system lock itself once you use one approach?
-
Maybe because you forgot to move the cursor...
Code:
//res is a CRecordset * that is at the first row//
//get the field by name//
CString sSourceField = "SOMEFIELD";
CString sSourceValue;
res->GetFieldValue(sSourceField,sSourceValue);
// ...here...
res->MoveNext();
//now loop through by index to get all the fields//
long nFields = res->GetODBCFieldCount();
for (long nIndex = 0; nIndex < nFields; nIndex++)
{
CString sValue;
res->GetFieldValue(nIndex,sValue);
//Fails to get "SOMEFIELD"'s value. Throws a CDBException
//but has no description of the error!//
}
-
Just after getting the record, move the cursor to next record.
So that once you have fetched the current record, your cursor should point to next record. If this also doesn't work inform me, i will look again.
-
What a riot! That did the trick!
I even called res->Move(0) and it fixed the problem. Talk about an undocumented bug!
Anyone want to trace it out and figure out why THAT happens? Why can you not GetFieldValue by fieldname first, then by index after?