Sunday, June 21, 2009

CRecordset::Open() - Invalid cursor state

When you call a stored procedure in C++ (MFC) through CRecordset
and the stored procedure does NOT return any result set,you
will get the following DBException :
"Invalid cursor state"

This happens because CRecordset expects a result set

and the Open() method automatically calls MoveFirst() method
that moves to the first record once the recordset is opened.

To workaround this issue you can do one of the following:
  1. Override CRecordset::Open() to prevent calling the code that moves to the first record once the recordset is opened.
  2. Change the SP so it will return empty record set.
  3. Use CDatabase::m_hdbc and do your own binding of input parameters.
  4. Use CDatabase::ExecuteSQL() instead of CRecordset class.

More information: http://support.microsoft.com/kb/137814

No comments:

Post a Comment