Wednesday, August 14, 2013

ASP.NET MVC and SQL Server Reporting Services

ASP.NET MVC and SQL Server Reporting Services If you need to view a SSRS type report in ASP.NET MVC application one way would be to have a Controller action method to launch report and an ASP.NET Web Form to host the Report Viewer control. Then the action method redirects to the .aspx page which contains the report viewer control and passes necessary parameters for report's query filter. The sample code is as following:

".ASPX web form page who contains ReportViewer control"
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Font-Size="8pt" Height="768px" ProcessingMode="Remote" WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Width="1024px"> <ServerReport ReportPath="/REPORTS_FOLDER/REPORT_NAME" ReportServerUrl="http://YOUR_SSRS_SERVER/reportserver" /> </rsweb:ReportViewer> </form> </body> </html>

using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.Reporting.WebForms; ... protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { var rprtPrms = (Dictionary) Session["reportParameters"]; foreach (var item in rprtPrms) { ReportViewer1.ServerReport.SetParameters( new List() { new ReportParameter(item.Key, item.Value.ToString()) } ); } } }

"The action in the controller"
public ActionResult Index() { var rprtPrms = new Dictionary(); rprtPrms.Add("MyReportParameter", 6); Session["reportParameters"] = rprtPrms; return Redirect("/RprtViewer.aspx"); }

Share/Bookmark

No comments: