SIGN UP MEMBER LOGIN:    
Blog

Passing parameters to Crystal Reports at runtime with c#

Posted by Shashi Ray Blogs | ASP.NET Programming Jan 21, 2008
This blog explains you how to pass parameters to crystal reports at runtime while using 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";

        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;

    }

}

share this blog :
post comment
 

When I have placed this code I got this exception "Object reference not set to an instance of an object." please tell me the solution of this problem because it is my urgent task, I don't have idea where i doing mistak.. you can email me at : irfan.lateef@hotmail.com .. please help me..

Thanks in advance

Posted by Irfan Lateef Oct 16, 2008

    protected void Page_Load(object sender, EventArgs e)
    {
        CrystalDecisions.Shared.ConnectionInfo myConnectionInfo = new CrystalDecisions.Shared.ConnectionInfo();
        myConnectionInfo.ServerName = ConfigurationManager.AppSettings["dtServerName"];


        myConnectionInfo.DatabaseName = ConfigurationManager.AppSettings["dtName"];
        myConnectionInfo.UserID = ConfigurationManager.AppSettings["dtUser"];


        myConnectionInfo.Password = ConfigurationManager.AppSettings["dtPwd"];

        ReportParameter();

    }
    private void ReportParameter()
    {

        CrystalReportViewer1.RefreshReport();

        ParameterFields paramFields = new ParameterFields();

        ParameterField pfItemYr = new ParameterField();

        pfItemYr.ParameterFieldName = "FormNo";

        ParameterDiscreteValue dcItemYr = new ParameterDiscreteValue();

        dcItemYr.Value = Request.QueryString["formno"].ToString();

        pfItemYr.CurrentValues.Add(dcItemYr);

        paramFields.Add(pfItemYr);

        CrystalReportViewer1.ParameterFieldInfo = paramFields;

    }

Posted by Irfan Lateef Oct 16, 2008