Dynamically Pass Parameter Value in Crystal Report

Dynamically Pass Parameter Value in Crystal Report

Dynamically Pass Parameter Value in Crystal Report

Firstly, To pass the parameter value dynamically at run time, first, we have to create a parameter field in crystal report by using Parameter Fields option from Field Explorer.

As you can see in the below image, I have taken a designation table from the database and added to the selected table list

Dynamically Pass Parameter Value in Crystal Report

After that, I had selected a column from the table and show them in the details section of the crystal report.

Dynamically Pass Parameter Value in Crystal Report

For creating a parameter field right clicks on the Parameter Fields option and click on New option to create a new parameter field.

Dynamically Pass Parameter Value in Crystal Report

In the new parameter section of the crystal report, give the name of the parameter and click OK. Here I gave a designation as a parameter name.

Dynamically Pass Parameter Value in Crystal Report

After that take Record selection formula from the special field in the crystal report and then right-click on the Record selection formula and Select Expert à Record.

Dynamically Pass Parameter Value in Crystal Report

After Select Expert à Record Choose Field dialog box will open, select the field from the table on the basis of which you want to display the report and click OK. Here I selected an employee_designation field as a base.

Dynamically Pass Parameter Value in Crystal Report

After that Select Expert – Record window will open, select the parameter field from the right Dropdown list and also select the is equal to option from the left Dropdown list if you want to show the details according to parameter value passed.

Dynamically Pass Parameter Value in Crystal Report

Design application form as shown below by using a label, textbox, button, and crystalReportViewer control from the toolbox.

Dynamically Pass Parameter Value in Crystal Report

After putting controls on form let’s move to the coding part and on the click event of button write the following code to pass the value in the parameter field.

    Private void button1_Click(object sender, EventArgs e)

     {
    {
              //creating an object of Report Document class
            ReportDocument reportDocument = new ReportDocument();

              //creating an object of ParameterField class
            ParameterField paramField = new ParameterField();

              //creating an object of ParameterFields class
            ParameterFields paramFields = new ParameterFields();

              //creating an object of ParameterDiscreteValue class
            ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();

     //set the parameter field name
            paramField.Name = 'designation';

              //set the parameter value
            paramDiscreteValue.Value = textBox1.Text;

              //add the parameter value in the ParameterField object
            paramField.CurrentValues.Add(paramDiscreteValue);

              //add the parameter in the ParameterFields object
            paramFields.Add(paramField);

              //set the parameterfield information in the crystal report
            crystalReportViewer1.ParameterFieldInfo = paramFields;

              //preparing root for preview
            reportDocument.Load('D:\\rohit\\crystal reports\\CrystalReport1.rpt');
            crystalReportViewer1.ReportSource = reportDocument;
            reportDocument.SetDatabaseLogon('userid','password','servername',
                                             'database');
         }


Dynamically Pass Parameter Value in Crystal Report

Here we can get output according to the parameter passed for employee_designation field.


Last updated:7/24/2020 10:33:55 PM
Chris Anderson

Chris Anderson

hi I am software developer at mindstick software pvt. ltd.

6 Comments

Muzaffar Mohammed
Muzaffar Mohammed

I want to use data directory of my project
reportDocument.Load("|DataDirectory|\\Forms\\CrystalReport1.rpt");    
to use after deployment
plz help me

Chris Anderson
Chris Anderson


You are missing space between "Data Directory"
reportDocument.Load("|Data Directory|\\Forms\\CrystalReport1.rpt");   

Muzaffar Mohammed
Muzaffar Mohammed

I am getting load report failed ,,,,,, if I use
reportDocument.Load("|Data Directory|\\Forms\\readngrpt.rpt");   // reportDocument.Load("|DataDirectory|
and if i give full address it is working
reportDocument.Load("G:\\Muzaffar\\Amber2000plus6BoxAccess\\Amber\\Forms\\readngrpt.rpt");

Chris Anderson
Chris Anderson

If you are using crystal report in winforms, you have to specify the path of the crystal report(.rpt).
Whatever I know we can not get full path just by using Data Directory.
Thanks.

sridhar sri
sridhar sri

I get the error"Load Report Failed".I used full path of the report like"C:Users\\innovit7\\Desktop\\26.12.2015\\PayRoll123\\PayRoll\\EmployeeCrystalReport.rpt" and also using Data Directory  like"|Data Directory|\\PayRoll\\EmployeeCrystalReport.rpt".But i still get the error.How can i Solbve it.Please help me....
Thanks in Advance

ashish mishra
ashish mishra

why are not show  question in the report...using window form.

my code

 DataSet ds = new DataSet();

                ReportDocument rptDoc = new ReportDocument();

                rptDoc.Load(@"D:/Jignesh_Work/Windows App/QuestionPaper/Ass3/Ass3/ViewQuestionNew.rpt");

                rptDoc.Database.Tables["GenerateQuestion"].SetDataSource(dt);

                rptDoc.Database.Tables["SchoolEntry"].SetDataSource(dt1);

                rptDoc.Database.Tables["QuestionPaper"].SetDataSource(dt3);

                for (int i = 0; i < dgvViewque.Rows.Count; i++)

                {

                    rptDoc.SetParameterValue("Question", dgvViewque.Rows[i].Cells[0].Value.ToString());

                    rptDoc.SetParameterValue("Date", dgvViewque.Rows[i].Cells["date"].Value.ToString());

                    rptDoc.SetParameterValue("Time", dgvViewque.Rows[i].Cells["time"].Value.ToString());

                    rptDoc.SetParameterValue("Class", dgvViewque.Rows[i].Cells["class"].Value.ToString());

                    rptDoc.SetParameterValue("Sub", dgvViewque.Rows[i].Cells["subject"].Value.ToString());

                    rptDoc.SetParameterValue("Mark", dgvViewque.Rows[i].Cells["mark"].Value.ToString());

                    rptDoc.SetParameterValue("School", dt1.Rows[0]["name"].ToString());

                    rptDoc.SetParameterValue("Address", dt1.Rows[0]["address"].ToString());

                    crystalReportViewer1.ReportSource = rptDoc;

                    crystalReportViewer1.Show();

                }

Leave Comment