How to set Parameters to Report in ReportViewer at Runtime?
When you want to pass the report parameters to the RDL/RDLC reports programmatically while a report is being loaded and a sub report is being processed, Syncfusion ASP.NET MVC Classic version of ReportViewer has support to programmatically set report parameters to report and this section helps you in learning how to set parameters to report programmatically in ReportViewer control.
When you are new to ASP.NET MVC Classic version of ReportViewer, you can refer to the Getting Started with ASP.NET MVC Classic version of ReportViewer from the following article.
Set Parameters Programmatically to Report
You can add collection of report parameters programmatically using the ReportViewerModel’s Parameters property that is the list type of ReportParameter class. The ReportParameter contains Name, Values, Labels, and Nullable properties to specify name, values and labels of report parameter.
Use the following code to set parameters.
C#
public class ReportViewerController : Controller { private ReportViewerModel GetInvoiceModel() { //Model data class for report viewer ReportViewerModel reportModel = new ReportViewerModel(); //Set the ReportPath of the Report reportModel.ReportPath = HttpContext.Server.MapPath("~/App_Data/InvoiceTemplate.rdl"); List<ReportParameter> parameters = new List<ReportParameter>(); ReportParameter parameter = new ReportParameter(); parameter.Name = "InvoiceID"; parameter.Labels.Add("10250"); parameter.Values.Add("10250"); //Add parameter parameters.Add(parameter); //Set the parameter collection reportModel.Parameters = parameters; return reportModel; } public ActionResult Index() { //Assign the ReportViewerModel to ViewData for ReportViewer Control ViewData["ReportModel"] = this.GetInvoiceModel(); return View(); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(ReportViewerParams param) { //Process the report request from client and send the action result return new ReportViewerHtmlActionResult(this.GetInvoiceModel(), param); } }
Use the following code to set parameters for Sub Report.
C#
public class ReportViewerController : Controller { private ReportViewerModel GetReportModel () { //Model data class for report viewer ReportViewerModel reportModel = new ReportViewerModel(); //Set the ReportPath of the Report reportModel.ReportPath = HttpContext.Server.MapPath("~/App_Data/SubReport.rdlc"); //Set the ProcessingMode of the Report reportModel.ProcessingMode = ProcessingMode.Local; //Hook event for SubReport Model reportModel.SubReport += model_subReport; return reportModel; } private void model_subReport(object sender, SubreportProcessingEventArgs e) { //Set the parameter collection e.Parameters = new ReportParameterInfoCollection(); ReportParameterInfo parameter = new ReportParameterInfo(); parameter.Name = "InvoiceID"; parameter.Labels.Add("10250"); parameter.Values.Add("10250"); //Add parameter e.Parameters.Add(parameter); } public ActionResult Index() { //Assign the ReportViewerModel to ViewData for ReportViewer Control ViewData["ReportModel"] = this. GetReportModel(); return View(); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(ReportViewerParams param) { //Process the report request from client and send the action result return new ReportViewerHtmlActionResult(this. GetReportModel (), param); } }
Demo Sample
You can download the Demo Sample from the following link.
https://www.syncfusion.com/downloads/support/directtrac/general/ReportViewerSample-2061069698.zip
Further References
You can find documentation and online demo samples for ASP.NET MVC Classic ReportViewer control from the following links.
Newer ASP.NET MVC version of ReportViewer
You can try the newer version of MVC ReportViewer that is powered by JavaScript/HTML5. You can refer to the documentation and online demo samples for newer ASP.NET MVC ReportViewer control from the following links.