Saturday, May 10, 2008

How to pass parameter to crystal report from ASP.NET

Suppose there is a crystal report which expects parameter id and section. Here’s the code that shows how to pass the parameter through ASP.net
Before this please do not forget to use following namespaces

using CrystalDecisions.Reporting;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using CrystalDecisions.Web;

private void displayReport(number id,string section)
{
try
{

CrystalReportViewer1.EnableParameterPrompt = false;
ParameterFields pFields = new ParameterFields();
ParameterField pField1 = new ParameterField();
ParameterField pField2 = new ParameterField();
ParameterDiscreteValue pDisValue1 = new ParameterDiscreteValue();
ParameterDiscreteValue pDisValue2 = new ParameterDiscreteValue();
pField1.Name = "id";
pField2.Name = "section";
pDisValue1.Value = id;
pField1.CurrentValues.Add(pDisValue1);
pFields.Add(pField1);
pDisValue2.Value = section;
pField2.CurrentValues.Add(pDisValue2);
pFields.Add(pField2);
CrystalReportViewer1.ParameterFieldInfo = pFields;
}
Catch
{
}
}

No comments: