Home > DeveloperSection > Beginner > Dynamically Pass Parameter Value in Crystal Report

Dynamically Pass Parameter Value in Crystal Report


Crystal Report Crystal Report 
Ratings:
5 Comment(s)
 32380  View(s)
Rate this:

Dynamically Pass Parameter Value in Crystal Report

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 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 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 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 parameter passed for employee_designation field.


data directory of my project in report load

By Muzaffar Mohammed on   4 years ago
I want to use data directory of my project

reportDocument.Load("|DataDirectory|\\Forms\\CrystalReport1.rpt");    
to use after deployment

plz help me

data directory of my project in report load

By Rohit Kesharwani on   4 years ago

You are missing space between "Data Directory"

reportDocument.Load("|Data Directory|\\Forms\\CrystalReport1.rpt");   

data directory of my project in report load

By Muzaffar Mohammed on   4 years ago
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");

data directory of my project in report load

By Rohit Kesharwani on   4 years ago
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.

Load Report Failed

By sridhar sri on   11 months ago
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

Don't want to miss updates? Please click the below button!

Follow MindStick