Link to home
Start Free TrialLog in
Avatar of trustworthy
trustworthy

asked on

CRecordset "Invalid Descriptor Index "

Hi all,

Here's my code:

SQL = SELECT ID,X,Y,A,TYPEID, vid  FROM (SELECT ID,A,X,Y,TYPEID,  (SELECT TOP 1 ID FROM DETAILS d WHERE pos.ID = d.ID) AS vid, row_number() over (partition by ID order by CREATEDTIME desc) as rn FROM POST pos) AS ss WHERE rn = 1;

CDatabase* pDatabase = new CDatabase();
pDatabase->Open(_T(""),FALSE,FALSE,LPCTSTR(sDsn));
CRecordset rs( pDatabase );
rs.m_bUseODBCCursorLib = TRUE;
BOOL iReturn = rs.Open(SQL ,CRecordset::forwardOnly,CRecordset::readOnly);
while(!rs.IsEOF() )
{
    CDBVariant v;
    memset(&v,0,sizeof(CDBVariant));
            
  rs.GetFieldValue((short)0, v, SQL_C_SLONG);
   //always the ERROR here
}

I searched high and low, but so far unable to solve this.

Maybe there's something wrong with my SQL? But
this only happens on SQLServer (I tried different databases on different versions).
Works fine in DB2.

Please help.
Avatar of jkr
jkr
Flag of Germany image

Is that really your actual code? I am asking because

BOOL iReturn = rs.Open(SQL ,CRecordset::forwardOnly,CRecordset::readOnly);

Open in new window


really seems wrong, all versions of 'CRecordset::Open()' take 'nOpenType' as their 1st parameter and the SQL statement as the 2nd, see the docs at http://msdn.microsoft.com/en-us/library/1hkkwdf0%28v=vs.120%29.aspx

Also, what is the return value? Are you checking that one before calling 'GetFieldValue()'?
Avatar of trustworthy
trustworthy

ASKER

You're right, I copied the wrong statement, it is:

rs.Open(CRecordset::forwardOnly,sp_text,CRecordset::readOnly);

And yes, I do check the return value, and I see number of records returned in the rs properties.
OK, and when you open the Data Set Visualizer in VS (just hover over the record set in the debugger and click on the magnifier symbol), do you get the results that you expected n terms of that query?
Yes, exactly what is expected. Same number of rows if I run the same query in any SQL program.
ASKER CERTIFIED SOLUTION
Avatar of trustworthy
trustworthy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bo other solution