Passing Parameters to Crystal Report in .Net

Now we are going to see, how we can show report by passing parameters or values. For example, Let us see how to retrieve details of an employee by means of Empid.

First Step

Create a table like the below structure.

Eg:

Column name Datatype
Name varchar(50)
Empid varchar(20)
Gender char(10)
Designation varchar(30)

Second Step

Create a crystal report and drag down your details. Create a parameter for empid. In the details right click the empid->select expert->select is equal to and select your parameter(here empid).

Third Step

Create a web or windows form add the control (Eg.dropdownlist, from where we are going to get the parameter) and add  crystalReportViewer in the same form.

Fourth Step

This is the time to add the code.

add the below packages to the code file,

using CrystalDecisions.CrystalReports.Engine;using CrystalDecisions.Shared;

add the below code to the Form_Load() event:

//setting the path of your crystal report

string path = “type the path of your report”;

this.crystalReportViewer1.ReportSource = path;

ReportDocument cryRpt = new ReportDocument();

cryRpt.Load(path);

//Creating parameters

ParameterFieldDefinitions crDefinitions;

ParameterFieldDefinition crDefinition;

ParameterValues crParameterValues = new ParameterValues();

ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

//Binding the parameter with class variable

crParameterDiscreteValue.Value = sample_class.empid; //you can save this value from your control

crDefinitions = cryRpt.DataDefinition.ParameterFields;

crDefinition = crDefinitions[“empid”];//Parameter Name which is same in the table

crParameterValues = crDefinition.CurrentValues;

crParameterValues.Clear();

crParameterValues.Add(crParameterDiscreteValue);

crDefinition.ApplyCurrentValues(crParameterValues);

crystalReportViewer1.ReportSource = cryRpt;

crystalReportViewer1.Refresh();

Run your program and select the parameter(here empid).It will show the details of an employee.

Have a happy coding..

Permanent link to this article: https://blog.openshell.in/2010/12/passing-parameters-to-crystal-report-in-net/

Leave a Reply

Your email address will not be published.