articles

Home / DeveloperSection / Articles / Dynamically Pass Parameter Value in Crystal Report

Dynamically Pass Parameter Value in Crystal Report

Dynamically Pass Parameter Value in Crystal Report

Chris Anderson54978 24-Aug-2011

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.



Updated 24-Jul-2020
hi I am software developer at mindstick software pvt. ltd.

Leave Comment

Comments

Liked By