CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2002
    Location
    Right here.
    Posts
    150

    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?

  2. #2
    Join Date
    May 2000
    Location
    Toronto, ON, Canada
    Posts
    3,573
    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!//
    }
    Regards,

    Emanuel Vaduva

  3. #3
    Join Date
    Dec 2001
    Location
    Chennai
    Posts
    101
    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.
    Tks
    Anupam

  4. #4
    Join Date
    Jan 2002
    Location
    Right here.
    Posts
    150
    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured