SIGN UP MEMBER LOGIN:    
ARTICLE

Passing parameters to Crystal Reports at runtime with c#

Posted by Shashi Ray Articles | Reports using C# September 29, 2008
This article shows how to pass parameters to a Crystl Reports at runtime in C#.
Reader Level:

This small article shows how you can create and pass parameters value at runtime in a Crystal Report from your UI. In my application, I am using a ReportViewer control in Visual Studio 2005 to view the report.

Note: To learn more about how to generate reports in a ReportViewer, read this free book: Free Book: Reports using Report Viewer in Visual Studio 2005

 

The main functionality is defined in the ReportParameter method listed below. In this method, I create a ParameterFields object, which is a collection of parameters.

 

  ParameterFields paramFields = new ParameterFields();

       

After that, I create ParameterField, set its name and values and the value I pass is from the DropDownlList on my page. You can pass value from any control you want.

 

  ParameterField pfItemYr = new ParameterField();

  pfItemYr.ParameterFieldName = "year"; //year is Crystal Report Parameter name.

  ParameterDiscreteValue dcItemYr = new ParameterDiscreteValue();

  dcItemYr.Value = DropDownList1.SelectedValue;

  pfItemYr.CurrentValues.Add(dcItemYr);

       

 

After that I add parameter to the ParameterFields. If you have multiple parameters, you can create and add them using the similar process.

 

    paramFields.Add(pfItemYr);

 

The last step is to set ParameterFieldInfo property of the CrystalReportViewer control to ParameterFields I created.

 

        CrystalReportViewer1.ParameterFieldInfo = paramFields;

   

 

Here is the complete code of the page.

 

using  CrystalDecisions.CrystalReports.Engine;

using   CrystalDecisions.Shared;
 

public partial class _Default : System.Web.UI.Page

{

    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Hello"]);

    Class1 dbc = new Class1();

 

    protected void Page_Load(object sender, EventArgs e)

    {

        CrystalReportViewer1.LogOnInfo[0].ConnectionInfo.ServerName = "ShashiKantRay";

        CrystalReportViewer1.LogOnInfo[0].ConnectionInfo.UserID = "sa";

        CrystalReportViewer1.LogOnInfo[0].ConnectionInfo.Password = "sa";

        CrystalReportViewer1.LogOnInfo[0].ConnectionInfo.DatabaseName = "Shashi";

        ReportParameter();

    }

    private void ReportParameter()

    {

        CrystalReportViewer1.RefreshReport();

        ParameterFields paramFields = new ParameterFields();

        ParameterField pfItemYr = new ParameterField();

        pfItemYr.ParameterFieldName = "year"; //year is Crystal Report Parameter name.

        ParameterDiscreteValue dcItemYr = new ParameterDiscreteValue();

        dcItemYr.Value = DropDownList1.SelectedValue;

        pfItemYr.CurrentValues.Add(dcItemYr);

        paramFields.Add(pfItemYr);

        CrystalReportViewer1.ParameterFieldInfo = paramFields;

    }

}

 

 

Note: To learn more about how to generate reports in a ReportViewer, read this free book: Free Book: Reports using Report Viewer in Visual Studio 2005

 

Login to add your contents and source code to this article
share this article :
post comment
 

I will test it, thanks.

Posted by Watcharakorn May 27, 2011

It's code is not run Success fully. pls help me. i want to pass parameter with c# in Crystal report.

Posted by rahul upadhyay Mar 21, 2011

hi all, ive used a parameter feild info property in my crystal report but even though for some records it is showing an error page with this (ParameterList - Use the ParameterFieldInfo property to specify parameter fields.) comment. but its working with other records. following is a sample of my code. reportDoc.SetDataSource(dsExample.Tables[0]); crvHAWBReport.RefreshReport(); ParameterFields paramFields = reportDoc.ParameterFields; paramFields["param"].DefaultValues.AddValue(dsAIR.Tables[0].Rows[0]["value1"].ToString()); paramFields["param"].DefaultValues.AddValue(dsAIR.Tables[0].Rows[0]["value2"].ToString()); paramFields["param"].DefaultValues.AddValue(dsAIR.Tables[0].Rows[0]["value3"].ToString()); paramFields["param"].DefaultValues.AddValue(dsAIR.Tables[0].Rows[0]["value4"].ToString()); crvHAWBReport.ParameterFieldInfo = paramFields; crvHAWBReport.ReportSource = reportDoc; crvHAWBReport.DataBind(); Page.DataBind(); pls can anyone suggest me a solution . deepa

Posted by deepa eswaran Oct 24, 2008

error is line pfItemYr.ParameterFieldName = "year"; //year is Crystal Report Parameter name.

Posted by dhirendra yadav Oct 01, 2008

Please specify how to add crystal report parameter "year"

Posted by Rajendra Deopura Sep 30, 2008
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor