About parameters for CR:
This is what we do in Powerbuilder. First we did this in the Open event of our window, but if there's a popup in the crystal report asking for parameters, this would enter in and endless loop of asking for the parameters. Therefore we now do this in an event POSTED from the Open() event:
setpointer(hourglass!)
try
ole_crviewer.object.OpenReport(is_archive, 1, ls_server, ls_user, ls_pwd, is_window, ls_query, ls_install, ls_user_pass, ls_cust_pass, ls_database)
Catch (OLERuntimeError cr_rptsrc)
// gnv_app.inv_error.of_message("Crystal Report", cr_rptsrc.getMessage() )
setpointer(arrow!)
// this.event pfc_close( )
RETURN
End try
Then in C# in the Crystal report viewer DLL we have this code for OpenReport():
public void OpenReport(string as_template, int ai_exclusive, string as_servername, string as_user,
string as_pwd, string as_window,
string as_query, string as_install, string as_user_pass, string as_cust_pass,
string as_database)
{
int li_cont=0;
List<string> list_parmNames = new List<string>();
cryRpt.Load(as_template);
TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
TableLogOnInfo crTableLogonInfo = new TableLogOnInfo();
ConnectionInfo crConnectionInfo = new ConnectionInfo();
Tables CrTables;
Tables CrSubRepTables;
ParameterFieldDefinitions crParameterFieldDefinitions;
ParameterValues crParameterValues = new ParameterValues();
//-- store all parameters in an array (used later on):
crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
foreach (CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinition crParmFld in crParameterFieldDefinitions)
{
list_parmNames.Add(crParmFld.ParameterFieldName);
li_cont++;
}
string[] ls_parmNames = list_parmNames.ToArray();
//-- logon all connections:
crConnectionInfo.ServerName = as_servername;
crConnectionInfo.DatabaseName = as_database; // u1, mjl, 10/04/17.
crConnectionInfo.UserID = as_user;
crConnectionInfo.Password = as_pwd;
CrTables = cryRpt.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
{
crTableLogonInfo = CrTable.LogOnInfo;
crTableLogonInfo.ConnectionInfo = crConnectionInfo;
CrTable.ApplyLogOnInfo(crTableLogonInfo);
}
//-- logon all connections of the sub-reports:
Sections crSections;
ReportObjects crReportObjects;
SubreportObject crSubreportObject;
ReportDocument crSubreportDocument;
//set the crSections object to the current report's sections
crSections = cryRpt.ReportDefinition.Sections;
//loop through all the sections to find all the report objects
foreach (Section crSection in crSections)
{
crReportObjects = crSection.ReportObjects;
//loop through all the report objects to find all the subreports
foreach (ReportObject crReportObject in crReportObjects)
{
if (crReportObject.Kind == ReportObjectKind.SubreportObject)
{
//you will need to typecast the reportobject to a subreport
//object once you find it
crSubreportObject = (SubreportObject)crReportObject;
//open the subreport object
crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
//set the database and tables objects to work with the subreport
CrSubRepTables = crSubreportDocument.Database.Tables;
//loop through all the tables in the subreport and
//set up the connection info and apply it to the tables
foreach (Table CrTable in CrSubRepTables)
{
crTableLogonInfo = CrTable.LogOnInfo;
crTableLogonInfo.ConnectionInfo = crConnectionInfo;
CrTable.ApplyLogOnInfo(crTableLogonInfo);
}
}
}
}
//////////////////////////////////////////
// set paramaters
//Log 2802,2803,2804 - Make sure parameters being passed exist - Patrick 24/01/06
switch (as_window)
{
case "qprint":
for (int i = 0; i < li_cont; i++)
{
if (ls_parmNames[i].ToLower() == "query")
{
cryRpt.SetParameterValue("query", as_query);
break;
}
}
break;
case "cprint":
for (int i = 0; i < li_cont; i++)
{
if (ls_parmNames[i].ToLower() == "contactno")
{
cryRpt.SetParameterValue("contactno", as_install); //&& we're not sure yet about this one
break;
}
}
break;
case "iprint":
for (int i = 0; i < li_cont; i++)
{
if (ls_parmNames[i].ToLower() == "install")
{
cryRpt.SetParameterValue("install", as_install);
break;
}
}
break;
default:
int li_ret, li_ret1;
if (!(as_user_pass == null) || !(as_cust_pass == null))
{
li_ret = 0; li_ret1 = 0;
if (!(as_cust_pass == null))
{
for (int i = 0; i < li_cont; i++)
{
if (ls_parmNames[i].ToLower() == "customer")
{
cryRpt.SetParameterValue("customer", as_cust_pass);
li_ret = 1;
break;
}
}
}
if (!(as_user_pass == null))
{
for (int i = 0; i < li_cont; i++)
{
if (ls_parmNames[i].ToLower() == "user")
{
cryRpt.SetParameterValue("user", as_user_pass);
li_ret1 = 1;
break;
}
}
}
if (li_ret == 0 && li_ret1 == 0)
{
//gnv_app.inv_error.of_message("no_parameters_exec_rep")
}
}
else
{
//cryRpt.DiscardSavedData;
bool autoSaveData = cryRpt.ReportOptions.EnableSaveDataWithReport;
if (!autoSaveData && cryRpt.HasSavedData)
{
MessageBox.Show("Discarding unsaved data in the report");
}
cryRpt.Refresh(); //&& not sure if this works all correctly for the DiscardSavedData;
cryRpt.VerifyDatabase(); //&& what is this good for ?
}
break;
}
// show the report
crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh();
}
Hope it might give you any useful information.
regards.