Search code examples
.netasp.netcrystal-reportswebforms

Passing parameters to Crystal report from webforms


I have a report and I want to display it on a webform. Reports without parameters are working well. Reports with parameters are creating headache for me. This is the code I have written in BindReport method, which is called on page load event of the form.

    ReportDocument rpt = new ReportDocument();
    rpt.Load(Server.MapPath("rptPositionwiseMontwiseActualSale.rpt"));
    rpt.FileName = Server.MapPath("rptPositionwiseMontwiseActualSale.rpt");
    rpt.SetParameterValue("CompanyName", "Cirin Pharmaceutical Pvt. Limited".ToString());
    rpt.SetParameterValue("ReportTitle", "PositionWise MonthWise Sales".ToString());
    rpt.SetParameterValue("parameters", "Year:2011".ToString());
    //CrystalReportViewer1.ParameterFieldInfo = paramFields;
    DataSet ds = getReportData();
    rpt.SetDataSource(ds.Tables[0]);
    CrystalReportViewer1.ReportSource = rpt;
    CrystalReportViewer1.ReuseParameterValuesOnRefresh = true;
   
    //CrystalReportViewer1.RefreshReport();

I have tried variety of things like assigning ParameterFieldInfo to reportviewer control, but it shows me prompt on page load asking for parameter values of the report. I'm using .NET 4.0.

Edit

I'm using push model for Crystal Reports. Does it change the way we can pass parameters to report from asp.net?


Solution

  • When setting Parameter values at runtime for ASP.NET I think you need to do a bit more work than just called SetParameterValue

    string rptTitle = "This is Report Title";
    
    rpt.SetParameterValue("ReportTitle", rptTitle);
    
    ParameterDiscreteValue val = new ParameterDiscreteValue();
    val.Value = rptTitle;
    
    ParameterValues paramVals = new ParameterValues();
    
    paramVals.Add(val);
    
    rpt.ParameterFields["ReportTitle"].CurrentValues = paramVals;
    
    rpt.DataDefinition.ParameterFields[0].ApplyCurrentValues(paramVals);
    

    This is probably a bit of overkill but it does actually work, well for me anyway. You must ensure that the parameter names match exactly.