Passing parameters to Crystal Reports at runtime with c#

If you take a look at the end of the Page_Load function, we have called a method ReportParameter(). This method will actually add/send the parameters to our report.


using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

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


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

    Class1 dbc = new Class1();


    protected void Page_Load(object sender, EventArgs e)


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

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

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

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



    private void ReportParameter()



        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;



        CrystalReportViewer1.ParameterFieldInfo = paramFields;