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 GenExcep->Delete(); } //CleanUp the objects if(m_pCommand!=NULL) { m_pCommand.Release(); } if(m_pConnection!=NULL) { m_pConnection->Close(); m_pConnection.Release();
-
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();
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>
-
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();
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