private ReportDocument mainReport;
protected void Page_Init(object sender, EventArgs e)
{
//report loaded for first time
if (!IsPostBack || (ReportDocument)Session["Report"] == null)
{
mainReport = new ReportDocument();
mainReport.Load("C:\\Users\\userid\\Desktop\\application\\application.Web\\CrystalReport\\Reports\\CrystalReport1.rpt");
//automatically log in to database without prompting the user
CrystalReportHelper.ConfigureCrystalReport(mainReport);
DataSet ds1 = new DataSet("MovieDS");
DataTable table = new DataTable("MovieDS");
table.Columns.Add("TITLE", typeof(System.String));
table.Columns.Add("YEAR", typeof(System.Int32));
DataRow row1 = table.NewRow();
row1["TITLE"] = "title_of_movie";
row1["YEAR"] = 1992;
table.Rows.Add(row1);
ds1.Tables.Add(table);
mainReport.Subreports[0].SetDataSource(ds1);
//SET PARAMETERS
SubreportSettingParameters_RD(mainReport, "subReport", "TITLE_PARAM", value);
//remember loaded report
Session.Add("Report", mainReport);
}
//connect crystal report viewer to display loaded report in session
CrystalReportViewer1.ReportSource = (ReportDocument)Session["Report"];
////prevent prompting of parameters when report is opened
CrystalReportViewer1.EnableParameterPrompt = false;
}
private void SubreportSettingParameters_RD(ReportDocument testReport, String subreportName, String parameterFieldName, String parameterValue)
{
ParameterDiscreteValue dv = new ParameterDiscreteValue();
dv.Value = parameterValue;
ParameterFields parameterFields = testReport.ParameterFields;
ParameterField parameterField = parameterFields[parameterFieldName, subreportName];
parameterField.CurrentValues.Clear();
parameterField.CurrentValues.Add(dv);
}
I'm using Crystal Report in Visual Studio Professional 2017. Thank you!
Request clarification before answering.
Hi Thuy,
There is no subreport parameter collection, it's all one collection off the main report collection. Code should work if you set the report name to nothing rather than the subreport name.
On the download page I have a parameter test app that may help you.
https://wiki.scn.sap.com/wiki/display/BOBJ/Crystal+Reports%2C+Developer+for+Visual+Studio+Downloads
Don
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This works!
Now I'm running into another issue: optional parameters. EDIT: Solved this issue with mainReport.ParameterFields["PARAM_NAME"].CurrentValues.IsNoValue = true;
Thanks for your help, Don!
//original: SubreportSettingParameters_RD(mainReport, subReport, "param", "parama_value");
//change to this
SubreportSettingParameters_RD(mainReport, "", "PARAMETER_NAME", value);<br>
| User | Count |
|---|---|
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.