borg48
asked on
Trying to pass a parameter to the IReport object
Here is my code. It works fine b/c i can get it to print without passing a parameter but now i want to
try and pass 1 parameter.
_variant_t varParameter;
varParameter.vt = VT_BSTR;
_bstr_t bsString("@Application_Id; 2454276");
varParameter.bstrVal = bsString;
hr = spReport->ParameterFields- >Item[1]-> AddCurrent Value(varP arameter);
OR
hr = spReport->ParameterFields- >Item[0]-> AddCurrent Value(varP arameter);
but it keeps getting caught in the catch(_com_error &err) exception
The error is :
err: Type mismatch
__vfptr: const _com_error: vftable
m_hersult = -2147352571
m_pszMsg = <Bad Ptr>
...
Not sure if this is enough.
Any help would be appreciated.
try and pass 1 parameter.
_variant_t varParameter;
varParameter.vt = VT_BSTR;
_bstr_t bsString("@Application_Id;
varParameter.bstrVal = bsString;
hr = spReport->ParameterFields-
OR
hr = spReport->ParameterFields-
but it keeps getting caught in the catch(_com_error &err) exception
The error is :
err: Type mismatch
__vfptr: const _com_error: vftable
m_hersult = -2147352571
m_pszMsg = <Bad Ptr>
...
Not sure if this is enough.
Any help would be appreciated.
not sure what version or what your datasource is but try these links:
http://support.crystaldecisions.com/library/kbase/articles/c2011107.asp
http://support.crystaldecisions.com/library/kbase/articles/c2012761.asp
hth
matt
http://support.crystaldecisions.com/library/kbase/articles/c2011107.asp
http://support.crystaldecisions.com/library/kbase/articles/c2012761.asp
hth
matt
Did you add a parameter to the report?
mlmcc
mlmcc
ASKER
I guess this is my confussion. I have this report that when i open with
crystal reports designer i connect to the database using a odbc dsn named
FinanceReports. Then i pull up Data Explorer and in the options select
stored procedures. Which now allows me to pull up a list of stored procedures
which i select one called spr_sel_DraftInfo which takes one parameter named
@Application_Id.
I can run the report in the designer which propts me for the @Application_Id to
enter.
Now in the code is where i'm a bit confused. I dont really want to use the
ODBC DSN name i would rather pass it a ADO connection. How do i go about
using a ado connection, stored procedure name and pass the 1 parameter to
crystal reports?
Every sample that i have seen shows openning a connection and passing a sql.
Ex.
I open the ADO connection:
_ConnectionPtr spADOConn(__uuidof(Connect ion));
hr = spADOConn->Open( _bstr_t(strConnection.c_st r()), _bstr_t(strUser.c_str()), _bstr_t(strPassword.c_str( )), adOpenUnspecified );
if( FAILED(hr))
throw "Error connection to database";
spReport is my Report object of Crystal Report
and
spApplication is my Application object of Crystal report
How do i go about setting the connection, then the stored procedure then
the 1 parameter value (for ex. 223485 ) that the stored procedures
requires?
crystal reports designer i connect to the database using a odbc dsn named
FinanceReports. Then i pull up Data Explorer and in the options select
stored procedures. Which now allows me to pull up a list of stored procedures
which i select one called spr_sel_DraftInfo which takes one parameter named
@Application_Id.
I can run the report in the designer which propts me for the @Application_Id to
enter.
Now in the code is where i'm a bit confused. I dont really want to use the
ODBC DSN name i would rather pass it a ADO connection. How do i go about
using a ado connection, stored procedure name and pass the 1 parameter to
crystal reports?
Every sample that i have seen shows openning a connection and passing a sql.
Ex.
I open the ADO connection:
_ConnectionPtr spADOConn(__uuidof(Connect
hr = spADOConn->Open( _bstr_t(strConnection.c_st
if( FAILED(hr))
throw "Error connection to database";
spReport is my Report object of Crystal Report
and
spApplication is my Application object of Crystal report
How do i go about setting the connection, then the stored procedure then
the 1 parameter value (for ex. 223485 ) that the stored procedures
requires?
ASKER
I think i'm over thought this. The report uses a stored procedure so all i have to do is pass it the one parameters.
So i did the code below the problem is when i run the program it comes up and asks me to enter the value for the parameter i just set and when i do set it to 1 on and it creates the pdf. the pdf is corrupted making me think it cant connect to the database to get the data.
Any ideas on how to do this properly?
spReport = spApplication->OpenReport( reportFile , varDummy);
spReport->DiscardSavedData ();
if( FAILED( spReport->Database->Tables ->Item[1]- >SetLogOnI nfo(_bstr_ t(""), _bstr_t("Provider=SQLOLEDB .1;Persist Security Info=True;Initial Catalog=Daytona;Data Source=localhost"), _bstr_t("sa"), _bstr_t("3st00g3s")) ))
throw "Erorr";
_variant_t varInt;
varInt.vt = VT_I4;
varInt.lVal = 1;
spReport->ParameterFields- >Item[1]-> AddDefault Value( varInt );
But either its not logging int or something b/c it keeps asking me to set
So i did the code below the problem is when i run the program it comes up and asks me to enter the value for the parameter i just set and when i do set it to 1 on and it creates the pdf. the pdf is corrupted making me think it cant connect to the database to get the data.
Any ideas on how to do this properly?
spReport = spApplication->OpenReport(
spReport->DiscardSavedData
if( FAILED( spReport->Database->Tables
throw "Erorr";
_variant_t varInt;
varInt.vt = VT_I4;
varInt.lVal = 1;
spReport->ParameterFields-
But either its not logging int or something b/c it keeps asking me to set
ASKER
I think i'm over thought this. The report uses a stored procedure so all i have to do is pass it the one parameters.
So i did the code below the problem is when i run the program it comes up and asks me to enter the value for the parameter i just set and when i do set it to 1 on and it creates the pdf. the pdf is corrupted making me think it cant connect to the database to get the data.
Any ideas on how to do this properly?
spReport = spApplication->OpenReport( reportFile , varDummy);
spReport->DiscardSavedData ();
if( FAILED( spReport->Database->Tables ->Item[1]- >SetLogOnI nfo(_bstr_ t(""), _bstr_t("Provider=SQLOLEDB .1;Persist Security Info=True;Initial Catalog=Daytona;Data Source=localhost"), _bstr_t("sa"), _bstr_t("3st00g3s")) ))
throw "Erorr";
_variant_t varInt;
varInt.vt = VT_I4;
varInt.lVal = 1;
spReport->ParameterFields- >Item[1]-> AddDefault Value( varInt );
But either its not logging int or something b/c it keeps asking me to set
So i did the code below the problem is when i run the program it comes up and asks me to enter the value for the parameter i just set and when i do set it to 1 on and it creates the pdf. the pdf is corrupted making me think it cant connect to the database to get the data.
Any ideas on how to do this properly?
spReport = spApplication->OpenReport(
spReport->DiscardSavedData
if( FAILED( spReport->Database->Tables
throw "Erorr";
_variant_t varInt;
varInt.vt = VT_I4;
varInt.lVal = 1;
spReport->ParameterFields-
But either its not logging int or something b/c it keeps asking me to set
ASKER
OK I'm getting closer:
spReport = spApplication->OpenReport( reportFile , varDummy);
spReport->DiscardSavedData ();
int nTParamSize = spReport->ParameterFields- >Count;
string strTParam = spReport->ParameterFields- >Item[1]-> ParameterF ieldName;
if( FAILED( spReport->Database->Tables ->Item[1]- >SetLogOnI nfo(_bstr_ t(""), _bstr_t("Provider=SQLOLEDB .1;Persist Security Info=True;Initial Catalog=SampleDB;Data Source=localhost"), _bstr_t("sa"), _bstr_t("password")) ))
or
if( FAILED( spReport->Database->Tables ->Item[1]- >SetLogOnI nfo(_bstr_ t("wshb189 0"), _bstr_t("SampleDB"), _bstr_t("sa"), _bstr_t("password")) ))
throw "Erorr";
_variant_t varInt;
varInt.vt = VT_I4;
varInt.lVal = 1;
// Not using this causes type mismatch
//spReport->ParameterField s->Item[1] ->AddDefau ltValue( varInt );
hr = spReport->ParameterFields- >GetItemBy Name(_bstr _t(strTPar am.c_str() ))->AddCur rentValue( varInt);
IExportOptionsPtr spExportOptions;
spExportOptions = spReport->ExportOptions;
spExportOptions->FormatTyp e = (CRExportFormatType)type;
ProcessExportPages(spExpor tOptions, type );
spExportOptions->DiskFileN ame = destinationFile;
hr = spReport->Export();
It doesnt ask me for the parameter anymore but when it writes the pdf its 0 bytes so there is something
wrong. I started a small test with a database called SampleDB with one table and 1 stored procedure:
CREATE PROCEDURE [dbo].[spRetrieveApps]
@Type int
AS
select * from Applications where IdType = @Type;
GO
So not quite sure what the culprit is and require some help
thanks
spReport = spApplication->OpenReport(
spReport->DiscardSavedData
int nTParamSize = spReport->ParameterFields-
string strTParam = spReport->ParameterFields-
if( FAILED( spReport->Database->Tables
or
if( FAILED( spReport->Database->Tables
throw "Erorr";
_variant_t varInt;
varInt.vt = VT_I4;
varInt.lVal = 1;
// Not using this causes type mismatch
//spReport->ParameterField
hr = spReport->ParameterFields-
IExportOptionsPtr spExportOptions;
spExportOptions = spReport->ExportOptions;
spExportOptions->FormatTyp
ProcessExportPages(spExpor
spExportOptions->DiskFileN
hr = spReport->Export();
It doesnt ask me for the parameter anymore but when it writes the pdf its 0 bytes so there is something
wrong. I started a small test with a database called SampleDB with one table and 1 stored procedure:
CREATE PROCEDURE [dbo].[spRetrieveApps]
@Type int
AS
select * from Applications where IdType = @Type;
GO
So not quite sure what the culprit is and require some help
thanks
ASKER
I even created a small vb app it does seem the problem is when i pass the parameter value to the report:
Dim App As New Application
Dim Rep As New Report
On Error GoTo Err
Set Rep = App.OpenReport("C:\Project s\ReportMi ll.Interfa ces\docs\R MillTest.r pt")
' Set Rep = App.OpenReport("C:\Project s\ReportMi ll.Interfa ces\docs\B alance.rpt ")
Rep.DiscardSavedData
Dim nItem As Integer
Dim strPara As String
nItem = Rep.ParameterFields.Count
strPara = Rep.ParameterFields(1).Par ameterFiel dName
' Either one fails so cant see a difference
Call Rep.Database.Tables(1).Set LogOnInfo( "", "Provider=SQLOLEDB.1;Persi st Security Info=True;Initial Catalog=Daytona;Data Source=SampleDB", "sa", "password")
or
' Call Rep.Database.Tables(1).Set LogOnInfo( "wshb1890" , "SampleDB", "sa", "password")
Rep.ParameterFields.GetIte mByName(st rPara).Add CurrentVal ue (1)
Rep.ExportOptions.DiskFile Name = "c:\pdf_report.pdf"
Rep.ExportOptions.Destinat ionType = crEDTDiskFile
Rep.ExportOptions.FormatTy pe = crEFTPortableDocFormat
Rep.Export False
But if i leave out the both SetLogOnInfo and ParameterFileds and used a non database report it works great.
If i leave out the ParameterFileds so it will ask me to enter the value it still fails. Again to me it seems that i'm using the wrong code to set a stored procedure.
Help....
Thanks
Dim App As New Application
Dim Rep As New Report
On Error GoTo Err
Set Rep = App.OpenReport("C:\Project
' Set Rep = App.OpenReport("C:\Project
Rep.DiscardSavedData
Dim nItem As Integer
Dim strPara As String
nItem = Rep.ParameterFields.Count
strPara = Rep.ParameterFields(1).Par
' Either one fails so cant see a difference
Call Rep.Database.Tables(1).Set
or
' Call Rep.Database.Tables(1).Set
Rep.ParameterFields.GetIte
Rep.ExportOptions.DiskFile
Rep.ExportOptions.Destinat
Rep.ExportOptions.FormatTy
Rep.Export False
But if i leave out the both SetLogOnInfo and ParameterFileds and used a non database report it works great.
If i leave out the ParameterFileds so it will ask me to enter the value it still fails. Again to me it seems that i'm using the wrong code to set a stored procedure.
Help....
Thanks
ASKER
Finally figured it out:
IApplicationPtr spApplication(__uuidof(App lication)) ;
_variant_t varDummy;
varDummy.vt = VT_EMPTY;
_variant_t varPrompt;
varPrompt.vt = VT_BOOL;
varPrompt.boolVal = false;
spApplication->LogOnServer (_bstr_t(" P2SSQL.DLL "), _bstr_t("wshb1890"), _bstr_t("Daytona"), _bstr_t("sa"), _bstr_t("3st00g3s"));
spReport = spApplication->OpenReport( reportFile , varDummy);
spReport->DiscardSavedData ();
spReport->EnableParameterP rompting = varPrompt;
_variant_t varInt;
varInt.vt = VT_I4;
varInt.lVal = 0;
spReport->ParameterFields- >Item[1]-> SetCurrent Value(varI nt);
spReport->ExportOptions->D iskFileNam e = "c:\\pdf_report.pdf";
spReport->ExportOptions->D estination Type = crEDTDiskFile;
spReport->ExportOptions->F ormatType = crEFTPortableDocFormat;
spReport->Export(varPrompt );
IApplicationPtr spApplication(__uuidof(App
_variant_t varDummy;
varDummy.vt = VT_EMPTY;
_variant_t varPrompt;
varPrompt.vt = VT_BOOL;
varPrompt.boolVal = false;
spApplication->LogOnServer
spReport = spApplication->OpenReport(
spReport->DiscardSavedData
spReport->EnableParameterP
_variant_t varInt;
varInt.vt = VT_I4;
varInt.lVal = 0;
spReport->ParameterFields-
spReport->ExportOptions->D
spReport->ExportOptions->D
spReport->ExportOptions->F
spReport->Export(varPrompt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
_bstr_t bsString("@Application_Id;
Do i need to create one parameter for each?