Click here to Skip to main content
15,885,985 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: _com_error not caught by 'Try-Catch' code Pin
Mike Grove5-Nov-13 22:53
Mike Grove5-Nov-13 22:53 
AnswerRe: _com_error not caught by 'Try-Catch' code Pin
Aescleal5-Nov-13 22:36
Aescleal5-Nov-13 22:36 
GeneralRe: _com_error not caught by 'Try-Catch' code Pin
Mike Grove5-Nov-13 22:57
Mike Grove5-Nov-13 22:57 
GeneralRe: _com_error not caught by 'Try-Catch' code Pin
Aescleal5-Nov-13 23:38
Aescleal5-Nov-13 23:38 
Questionmfc Pin
shizhanbiao5-Nov-13 19:59
shizhanbiao5-Nov-13 19:59 
AnswerRe: mfc Pin
CPallini5-Nov-13 21:32
mveCPallini5-Nov-13 21:32 
QuestionRe: mfc Pin
David Crow6-Nov-13 4:13
David Crow6-Nov-13 4:13 
QuestionGetting output parameters value[Solved] Pin
pkfox4-Nov-13 4:04
professionalpkfox4-Nov-13 4:04 
Hi all, I read some more on MSDN and found out that if you call a stored proc ( SQL Server in my case ) which returns a record set and also populates an *output* parameter , the parameters return value is in a separate record set. What I did was print all my records then move to the next set with:
spRst = spRst->NextRecordset();
// you also need to access the *Item* collection ( which I wasn't doing previously )
// then get the return value from the *command object*
bstr_t NumRecs = ((bstr_t)pCmd->Parameters->Item["Count"]->Value)->GetBSTR(); // this used to always return zero.


A lesson learnt.


Hi all, the code below works fine except for the retrieval of the @Count parameter which is of type *output* it always returns zero - any ideas ? ( it's this line of code : _tprintf(_T("Records found = %d\n\n"),spCmd->Parameters["@Count"]);

Many thanks



C++


#pragma region Includes and Imports
#include "stdafx.h"

#include <atlstr.h>

#import "C:\Program Files\Common Files\System\ADO\msado15.dll" rename("EOF","EndOfFile")
using namespace ADODB;
#pragma endregion

static ADODB::_ParameterPtr CreateParam(_bstr_t Name, _bstr_t Value, int Size, ADODB::DataTypeEnum Type, ADODB::ParameterDirectionEnum Direction);
static ADODB::_ParameterPtr CreateParam(_bstr_t Name, int Value, int Size, ADODB::DataTypeEnum Type, ADODB::ParameterDirectionEnum Direction);

int _tmain(int argc, _TCHAR* argv[])
{
if (argc < 4)
{
_tprintf(_T("Usage: CppUseADO.exe \"Search for this\" Y|N to specify combined or not Y|N to include address search\n"));
return -1;
}

int i = 0;
::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

ADODB::_ConnectionPtr spConn = NULL;
ADODB::_RecordsetPtr spRst = NULL;
ADODB::_CommandPtr spCmd = NULL;
ADODB::_ParameterPtr spParam = NULL;

_bstr_t bstrSearchExp(argv[1]);
_bstr_t bstrCombined(argv[2]);
_bstr_t bstrSearchAddress(argv[3]);

_bstr_t bstrConn("dsn=CMS;");


try
{

spConn.CreateInstance(__uuidof(ADODB::Connection));
_tprintf(_T("Connecting to the database ...\n"));
spConn->Open(bstrConn, "", "", NULL);


spCmd.CreateInstance(__uuidof(ADODB::Command));

spCmd->ActiveConnection = spConn;
spCmd->CommandText = "SearchOnCreditorName";
spCmd->CommandType = ADODB::adCmdStoredProc;

spRst.CreateInstance(__uuidof(ADODB::Recordset));

spParam = CreateParam("@PartWord", bstrSearchExp, 35, ADODB::adVarChar, ADODB::ParameterDirectionEnum::adParamInput);
spCmd->Parameters->Append(spParam);

spParam = CreateParam("@Combined", bstrCombined, 1, ADODB::adChar, ADODB::ParameterDirectionEnum::adParamInput);
spCmd->Parameters->Append(spParam);

spParam = CreateParam("@SearchAddress", bstrSearchAddress, 1, ADODB::adChar, ADODB::ParameterDirectionEnum::adParamInput);
spCmd->Parameters->Append(spParam);

spParam = CreateParam("@Count", 0, 8, ADODB::adInteger, ADODB::ParameterDirectionEnum::adParamReturnValue);
spCmd->Parameters->Append(spParam);

_tprintf(_T("Searching the CMS Database for value %s \n"), (PCTSTR)bstrSearchExp);
spCmd->Prepared = true;

spRst = spCmd->Execute(NULL, NULL, NULL);


_tprintf(_T("Records found = %d\n\n"),spCmd->Parameters["@Count"]);


while (!spRst->EndOfFile)
{
variant_t vtClaimRef(spRst->Fields->Item["ClaimRef"]->Value);
variant_t vtCreditorName(spRst->Fields->Item["CreditorName"]->Value);
variant_t vtAddr1(spRst->Fields->Item["AddrLine1"]->Value);
variant_t vtAddr2(spRst->Fields->Item["AddrLine2"]->Value);
variant_t vtAddr3(spRst->Fields->Item["AddrLine3"]->Value);

_tprintf(_T("%s\t %s\t %s %s %s\n"),
vtClaimRef.vt == VT_NULL ? _T("(DBNull)") : (PCTSTR)vtClaimRef.bstrVal,
vtCreditorName.vt == VT_NULL ? _T("(DBNull)") : (PCTSTR)vtCreditorName.bstrVal,
vtAddr1.vt == VT_NULL ? _T("(DBNull)") : (PCTSTR)vtAddr1.bstrVal,
vtAddr2.vt == VT_NULL ? _T("(DBNull)") : (PCTSTR)vtAddr2.bstrVal,
vtAddr3.vt == VT_NULL ? _T("(DBNull)") : (PCTSTR)vtAddr3.bstrVal);

i++;
spRst->MoveNext(); // Move to the next record
}


_tprintf(_T("%d records printed for %s\n"), i, (PCTSTR)bstrSearchExp);


}
catch (_com_error err)
{
_tprintf(_T("\nOops something went wrong ! probably no records found\n"));
_tprintf(err.Description());
_tprintf(err.ErrorMessage());
_tprintf(_T("\n\n"));
}


/////////////////////////////////////////////////////////////////////////
// Clean up objects before exit.
//

_tprintf(_T("Closing the connection...\n"));

// Close the record set if it is open
if (spRst && spRst->State == ADODB::adStateOpen)
spRst->Close();

// Close the connection to the database if it is open
if (spConn && spConn->State == ADODB::adStateOpen)
spConn->Close();

::CoUninitialize();
return 0;
}


static ADODB::_ParameterPtr CreateParam(_bstr_t Name, _bstr_t Value, int Size, ADODB::DataTypeEnum Type, ADODB::ParameterDirectionEnum Direction)
{
ADODB::_ParameterPtr spParam;
spParam.CreateInstance(__uuidof(ADODB::Parameter));
spParam->Direction = Direction;
spParam->Name = Name;
spParam->Type = Type;
spParam->Size = Size;
spParam->Value = Value;
return spParam;
}

static ADODB::_ParameterPtr CreateParam(_bstr_t Name, int Value, int Size, ADODB::DataTypeEnum Type, ADODB::ParameterDirectionEnum Direction)
{
ADODB::_ParameterPtr spParam;
spParam.CreateInstance(__uuidof(ADODB::Parameter));
spParam->Direction = Direction;
spParam->Name = Name;
spParam->Type = Type;
spParam->Size = Size;
spParam->Value = Value;
return spParam;
}
C++


We can’t stop here, this is bat country - Hunter S Thompson RIP


modified 6-Nov-13 2:40am.

SuggestionRe: Gettin output parameters value Pin
Richard MacCutchan4-Nov-13 6:10
mveRichard MacCutchan4-Nov-13 6:10 
GeneralRe: Gettin output parameters value Pin
pkfox4-Nov-13 10:07
professionalpkfox4-Nov-13 10:07 
SuggestionRe: Gettin output parameters value Pin
David Crow4-Nov-13 10:44
David Crow4-Nov-13 10:44 
GeneralRe: Gettin output parameters value Pin
Richard MacCutchan4-Nov-13 22:29
mveRichard MacCutchan4-Nov-13 22:29 
GeneralRe: Gettin output parameters value Pin
Richard MacCutchan4-Nov-13 22:31
mveRichard MacCutchan4-Nov-13 22:31 
GeneralRe: Gettin output parameters value Pin
pkfox5-Nov-13 20:00
professionalpkfox5-Nov-13 20:00 
GeneralRe: Gettin output parameters value Pin
Richard MacCutchan5-Nov-13 21:37
mveRichard MacCutchan5-Nov-13 21:37 
GeneralRe: Gettin output parameters value Pin
pkfox6-Nov-13 3:05
professionalpkfox6-Nov-13 3:05 
QuestionCaptur Audio Using WASAPI Pin
AmbiguousName3-Nov-13 2:20
AmbiguousName3-Nov-13 2:20 
AnswerRe: Captur Audio Using WASAPI Pin
Jochen Arndt4-Nov-13 2:45
professionalJochen Arndt4-Nov-13 2:45 
Question[Sovled] Win32 C++ - Child Windows Pin
lrinish2-Nov-13 9:49
lrinish2-Nov-13 9:49 
SuggestionRe: Win32 C++ - Child Windows Pin
Richard MacCutchan2-Nov-13 21:27
mveRichard MacCutchan2-Nov-13 21:27 
AnswerRe: Win32 C++ - Child Windows Pin
pasztorpisti3-Nov-13 5:25
pasztorpisti3-Nov-13 5:25 
GeneralRe: Win32 C++ - Child Windows Pin
lrinish3-Nov-13 6:32
lrinish3-Nov-13 6:32 
GeneralRe: Win32 C++ - Child Windows Pin
pasztorpisti3-Nov-13 7:01
pasztorpisti3-Nov-13 7:01 
QuestionArithmetic operation in pointer using C Pin
shanmugarajaa31-Oct-13 20:20
shanmugarajaa31-Oct-13 20:20 
QuestionRe: Arithmetic operation in pointer using C Pin
Richard MacCutchan31-Oct-13 23:47
mveRichard MacCutchan31-Oct-13 23:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.