Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. RecordCount Property return me -1?

RecordCount Property return me -1?

Scheduled Pinned Locked Moved C / C++ / MFC
sharepointcomhelpquestionannouncement
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anju
    wrote on last edited by
    #1

    Hi,All RecordCount Property giving me -1 value Please read my code and give me where i am doing wrong; and also please comment on my Code(if their any mistakes) Thanks in Advance.. /////////////This is my code To Test the Function//////////////////// _RecordsetPtr pRst; CString strTemp; pRst.CreateInstance(__uuidof(Recordset)); pRst=GetData(1); if(pRst!=NULL) { int nCount=(int)pRst->RecordCount; strTemp.Format("%d",nCount); AfxMessageBox(strTemp); } else { AfxMessageBox("Recordset null"); } pRst.Close(); pRst.Release(); //////////////////////////////////// ///////////////////This is my funtion which will return me Recordset object/////////// _RecordsetPtr GetData(int nType) { CString strType; strType.Format("%d",nType); try { //COM Library Initialization if(FAILED(::CoInitialize(NULL))) { //Log error return NULL; }//if(FAILED(::CoInitialize(NULL)) //Open the Connection m_hr=m_pConnection.CreateInstance(__uuidof(Connection)); if(FAILED(m_hr)) { ::CoUninitialize(); //Log error return NULL; } m_strCnnString=GetConnectionString();//It will give me the Connection String _bstr_t cnnString(m_strCnnString); _bstr_t bstrEmpty(""); m_hr=m_pConnection->Open(cnnString,bstrEmpty,bstrEmpty,adConnectUnspecified); if(FAILED(m_hr)) { m_pConnection.Release(); ::CoUninitialize(); //Log error return NULL; } //Get the Command objet m_hr=m_pCommand.CreateInstance(__uuidof(Command)); if(FAILED(m_hr)) { m_pConnection->Close(); m_pConnection.Release(); ::CoUninitialize(); //Log error return NULL; } m_pCommand->ActiveConnection=m_pConnection; m_pCommand->CommandText="Sp_GetData"; m_pCommand->CommandType=adCmdStoredProc; //Append the Parameters to Command object //Type Parameter m_pParam=m_pCommand->CreateParameter(_bstr_t("Type"),adInteger,adParamInput,4,_variant_t(strType)); m_pCommand->Parameters->Append(m_pParam); m_hr=m_pRecordSet.CreateInstance(__uuidof(Recordset)); if(FAILED(m_hr)) { m_pConnection->Close(); m_pConnection.Release(); ::CoUninitialize(); //Log error return NULL; } //Execute the SP m_pRecordSet->CursorType=adOpenStatic; m_pRecordSet=m_pCommand->Execute(NULL,NULL,adCmdStoredProc); return m_pRecordSet; }//try block catch(_com_error &comExcep) { //Handle the error } catch(CException * GenExcep) { //Handle the error GenExcep->Delete(); } //CleanUp the objects if(m_pCommand!=NULL) { m_pCommand.Release(); } if(m_pConnection!=NULL) { m_pConnection->Close(); m_pConnection.Release();

    J N 2 Replies Last reply
    0
    • A anju

      Hi,All RecordCount Property giving me -1 value Please read my code and give me where i am doing wrong; and also please comment on my Code(if their any mistakes) Thanks in Advance.. /////////////This is my code To Test the Function//////////////////// _RecordsetPtr pRst; CString strTemp; pRst.CreateInstance(__uuidof(Recordset)); pRst=GetData(1); if(pRst!=NULL) { int nCount=(int)pRst->RecordCount; strTemp.Format("%d",nCount); AfxMessageBox(strTemp); } else { AfxMessageBox("Recordset null"); } pRst.Close(); pRst.Release(); //////////////////////////////////// ///////////////////This is my funtion which will return me Recordset object/////////// _RecordsetPtr GetData(int nType) { CString strType; strType.Format("%d",nType); try { //COM Library Initialization if(FAILED(::CoInitialize(NULL))) { //Log error return NULL; }//if(FAILED(::CoInitialize(NULL)) //Open the Connection m_hr=m_pConnection.CreateInstance(__uuidof(Connection)); if(FAILED(m_hr)) { ::CoUninitialize(); //Log error return NULL; } m_strCnnString=GetConnectionString();//It will give me the Connection String _bstr_t cnnString(m_strCnnString); _bstr_t bstrEmpty(""); m_hr=m_pConnection->Open(cnnString,bstrEmpty,bstrEmpty,adConnectUnspecified); if(FAILED(m_hr)) { m_pConnection.Release(); ::CoUninitialize(); //Log error return NULL; } //Get the Command objet m_hr=m_pCommand.CreateInstance(__uuidof(Command)); if(FAILED(m_hr)) { m_pConnection->Close(); m_pConnection.Release(); ::CoUninitialize(); //Log error return NULL; } m_pCommand->ActiveConnection=m_pConnection; m_pCommand->CommandText="Sp_GetData"; m_pCommand->CommandType=adCmdStoredProc; //Append the Parameters to Command object //Type Parameter m_pParam=m_pCommand->CreateParameter(_bstr_t("Type"),adInteger,adParamInput,4,_variant_t(strType)); m_pCommand->Parameters->Append(m_pParam); m_hr=m_pRecordSet.CreateInstance(__uuidof(Recordset)); if(FAILED(m_hr)) { m_pConnection->Close(); m_pConnection.Release(); ::CoUninitialize(); //Log error return NULL; } //Execute the SP m_pRecordSet->CursorType=adOpenStatic; m_pRecordSet=m_pCommand->Execute(NULL,NULL,adCmdStoredProc); return m_pRecordSet; }//try block catch(_com_error &comExcep) { //Handle the error } catch(CException * GenExcep) { //Handle the error GenExcep->Delete(); } //CleanUp the objects if(m_pCommand!=NULL) { m_pCommand.Release(); } if(m_pConnection!=NULL) { m_pConnection->Close(); m_pConnection.Release();

      J Offline
      J Offline
      Jon Hulatt
      wrote on last edited by
      #2

      from msdn: Use the RecordCount property to find out how many records are in a Recordset object. The property returns -1 when ADO cannot determine the number of records or if the provider or cursor type does not support RecordCount. Reading the RecordCount property on a closed Recordset causes an error. Basically, what you need to do is set the CursorLocation property of your recordset to adUseClient before opening it.

      #include <beer.h>

      1 Reply Last reply
      0
      • A anju

        Hi,All RecordCount Property giving me -1 value Please read my code and give me where i am doing wrong; and also please comment on my Code(if their any mistakes) Thanks in Advance.. /////////////This is my code To Test the Function//////////////////// _RecordsetPtr pRst; CString strTemp; pRst.CreateInstance(__uuidof(Recordset)); pRst=GetData(1); if(pRst!=NULL) { int nCount=(int)pRst->RecordCount; strTemp.Format("%d",nCount); AfxMessageBox(strTemp); } else { AfxMessageBox("Recordset null"); } pRst.Close(); pRst.Release(); //////////////////////////////////// ///////////////////This is my funtion which will return me Recordset object/////////// _RecordsetPtr GetData(int nType) { CString strType; strType.Format("%d",nType); try { //COM Library Initialization if(FAILED(::CoInitialize(NULL))) { //Log error return NULL; }//if(FAILED(::CoInitialize(NULL)) //Open the Connection m_hr=m_pConnection.CreateInstance(__uuidof(Connection)); if(FAILED(m_hr)) { ::CoUninitialize(); //Log error return NULL; } m_strCnnString=GetConnectionString();//It will give me the Connection String _bstr_t cnnString(m_strCnnString); _bstr_t bstrEmpty(""); m_hr=m_pConnection->Open(cnnString,bstrEmpty,bstrEmpty,adConnectUnspecified); if(FAILED(m_hr)) { m_pConnection.Release(); ::CoUninitialize(); //Log error return NULL; } //Get the Command objet m_hr=m_pCommand.CreateInstance(__uuidof(Command)); if(FAILED(m_hr)) { m_pConnection->Close(); m_pConnection.Release(); ::CoUninitialize(); //Log error return NULL; } m_pCommand->ActiveConnection=m_pConnection; m_pCommand->CommandText="Sp_GetData"; m_pCommand->CommandType=adCmdStoredProc; //Append the Parameters to Command object //Type Parameter m_pParam=m_pCommand->CreateParameter(_bstr_t("Type"),adInteger,adParamInput,4,_variant_t(strType)); m_pCommand->Parameters->Append(m_pParam); m_hr=m_pRecordSet.CreateInstance(__uuidof(Recordset)); if(FAILED(m_hr)) { m_pConnection->Close(); m_pConnection.Release(); ::CoUninitialize(); //Log error return NULL; } //Execute the SP m_pRecordSet->CursorType=adOpenStatic; m_pRecordSet=m_pCommand->Execute(NULL,NULL,adCmdStoredProc); return m_pRecordSet; }//try block catch(_com_error &comExcep) { //Handle the error } catch(CException * GenExcep) { //Handle the error GenExcep->Delete(); } //CleanUp the objects if(m_pCommand!=NULL) { m_pCommand.Release(); } if(m_pConnection!=NULL) { m_pConnection->Close(); m_pConnection.Release();

        N Offline
        N Offline
        Nick Parker
        wrote on last edited by
        #3

        See my answer in the SQL/ADO/ADO.NET Forum http://www.codeproject.com/script/comments/forums.asp?msg=345403&forumid=1725#xx345403xx[^] Nick Parker
        May your glass be ever full. May the roof over your head be always strong. And may you be in heaven half an hour before the devil knows you’re dead. - Irish Blessing


        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups