RecordCount Property return me -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 Ge
-
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 Ge
anju wrote: RecordCount Property giving me -1 value MSDN has an article: PRB: ADO: Recordcount May Return -1[^] You might also review the example on this site: Filter and RecordCount Properties Example (VC++)[^]. Here is a snip of what I am talking about.
rstPublishers->CursorType = adOpenStatic;
TESTHR( rstPublishers->Open("publishers",strCnn, adOpenStatic , adLockReadOnly,adCmdTable)); // Populate the Recordset. intPublisherCount = rstPublishers->RecordCount;
Hope this helps. :) 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
-
anju wrote: RecordCount Property giving me -1 value MSDN has an article: PRB: ADO: Recordcount May Return -1[^] You might also review the example on this site: Filter and RecordCount Properties Example (VC++)[^]. Here is a snip of what I am talking about.
rstPublishers->CursorType = adOpenStatic;
TESTHR( rstPublishers->Open("publishers",strCnn, adOpenStatic , adLockReadOnly,adCmdTable)); // Populate the Recordset. intPublisherCount = rstPublishers->RecordCount;
Hope this helps. :) 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
Hi Nick, Thanks for your reply, Still now RecordCount Property giving me -1; what your provided information is good if i am getting the recordset through "adCmdText or adCmdTable" but here i am getting the recordset through "adSotredProc". r u observed this difference? i am getting full recordset with out any problem except RecordCount. Actually my problem was concentrated here.... //Execute the SP m_pRecordSet->CursorType=adOpenStatic; m_pRecordSet=m_pCommand->Execute(NULL,NULL,adCmdStoredProc); return m_pRecordSet; ---In the above code.Eventhogh i assigned "adOpenStatic" to CursorType it is not replicating it is defaultly taking the "adOpenForwardOnly". ////////// _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(); ///////////////////////// ----Can u tell where i have to set the CursorType? Once again i am waiting for your reply... thanks in advance:rose::rose::rose: anju