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
After that, I had selected a column from the table and show them in the details section of the 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.
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.
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.
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.
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.
Design application form as shown below by using a label, textbox, button, and crystalReportViewer control from the toolbox.
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");
}
Here we can get output according to the parameter passed for employee_designation field.
ashish mishra
03-Nov-2018why 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();
}
sridhar sri
29-Dec-2015Chris Anderson
24-Jan-2012Muzaffar Mohammed
23-Jan-2012reportDocument.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
23-Jan-2012You are missing space between "Data Directory"
reportDocument.Load("|Data Directory|\\Forms\\CrystalReport1.rpt");
Muzaffar Mohammed
23-Jan-2012reportDocument.Load("|DataDirectory|\\Forms\\CrystalReport1.rpt");
to use after deployment
plz help me