This small article shows how you can create and pass parameters value at runtime in a Crystal Report from your UI. In my application, I am using a ReportViewer control in Visual Studio 2005 to view the report.
The main functionality is defined in the ReportParameter method listed below. In this method, I create a ParameterFields object, which is a collection of parameters.
ParameterFields paramFields = new ParameterFields();
After that, I create ParameterField, set its name and values and the value I pass is from the DropDownlList on my page. You can pass value from any control you want.
ParameterField pfItemYr = new ParameterField();
pfItemYr.ParameterFieldName = "year"; //year is Crystal Report Parameter name.
ParameterDiscreteValue dcItemYr = new ParameterDiscreteValue();
dcItemYr.Value = DropDownList1.SelectedValue;
pfItemYr.CurrentValues.Add(dcItemYr);
After that I add parameter to the ParameterFields. If you have multiple parameters, you can create and add them using the similar process.
paramFields.Add(pfItemYr);
The last step is to set ParameterFieldInfo property of the CrystalReportViewer control to ParameterFields I created.
CrystalReportViewer1.ParameterFieldInfo = paramFields;
Here is the complete code of the page.
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
public partial class _Default : System.Web.UI.Page

WatcharakornPosted May 27, 2011, 7:40 AM
I will test it, thanks.
rahul upadhyayPosted Mar 21, 2011, 9:02 AM
It's code is not run Success fully. pls help me. i want to pass parameter with c# in Crystal report.
deepa eswaranPosted Oct 24, 2008, 8:32 AM
hi all, ive used a parameter feild info property in my crystal report but even though for some records it is showing an error page with this (ParameterList - Use the ParameterFieldInfo property to specify parameter fields.) comment. but its working with other records. following is a sample of my code. reportDoc.SetDataSource(dsExample.Tables[0]); crvHAWBReport.RefreshReport(); ParameterFields paramFields = reportDoc.ParameterFields; paramFields["param"].DefaultValues.AddValue(dsAIR.Tables[0].Rows[0]["value1"].ToString()); paramFields["param"].DefaultValues.AddValue(dsAIR.Tables[0].Rows[0]["value2"].ToString()); paramFields["param"].DefaultValues.AddValue(dsAIR.Tables[0].Rows[0]["value3"].ToString()); paramFields["param"].DefaultValues.AddValue(dsAIR.Tables[0].Rows[0]["value4"].ToString()); crvHAWBReport.ParameterFieldInfo = paramFields; crvHAWBReport.ReportSource = reportDoc; crvHAWBReport.DataBind(); Page.DataBind(); pls can anyone suggest me a solution . deepa
dhirendra yadavPosted Oct 1, 2008, 7:15 AM
error is line pfItemYr.ParameterFieldName = "year"; //year is Crystal Report Parameter name.
Rajendra DeopuraeditedPosted Sep 30, 2008, 10:09 AMEdited Sep 30, 2008, 10:10 AM
Please specify how to add crystal report parameter "year"