Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Page History: Implementing a Report Viewer Page - ASP.NET MVC

Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: Tue, Jul 08, 2014, 1:57 PM


Overview

The ReportViewer control cannot be hosted on an ASP.NET MVC page - only a WebForms page. This article walks through how to use a ReportViewer control in a mostly-ASP.NET-MVC project.

Code

AppReport Database Table

Create a new dbo.AppReport table in your database. This will hold a list of all reports available on your site.

CREATE TABLE [dbo].[AppReport](
	[AppReportID] [int] IDENTITY(1,1) NOT NULL,
	[DisplayName] [nvarchar](100) NOT NULL,
	[IsDeleted] [bit] NOT NULL,
	[ReportFile] [nvarchar](100) NOT NULL,
 CONSTRAINT [PK_AppReport] PRIMARY KEY CLUSTERED 
(
	[AppReportID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
    ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Business Layer

Create a new business object called AppReportManager. Within it create a new method called ReportsForCurrentUser to return a list of reports for the currently logged-in user.

Razor View

Create a new ReportsController with a View method, then create a View.cshtml view with the following content.

@{
    ViewBag.Title = "Reports";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<iframe id="ReportIframe" src="/Reports/ReportViewer/aspx" style="width:100%; height:550px; border: 1px dotted silver;"></iframe>

====ReportViewer.aspx Page====
Create a {{Reports}} folder in the root of your web project, and create a new Web Forms page called {{ReportViewer.aspx}} within it with the following content.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ReportViewer.aspx.cs" Inherits="My.Namespace.Reports.ReportViewer" %>

<%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>



Report Viewer
Report:










====ReportViewer.aspx.cs Code Behind====
public partial class ReportViewer : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { InitReportDropDown(); } }

private void InitReportDropDown() { var reports = new AppReportManager().ReportsForCurrentUser();

uxReportDropDown.DataSource = null; uxReportDropDown.DataSource = reports; uxReportDropDown.DataBind(); }

protected void uxGoButton_Click(object sender, EventArgs e) { var reportName = uxReportDropDown.SelectedValue.ToString(); RenderServerReport(reportName); }

private void RenderServerReport(string reportName) { try { /*- Inits -*/ var rv = uxReportViewer; var rpt = rv.ServerReport; ReportParameterInfo p = null;

/*- Configure ReportViewer Control -*/ rv.ShowCredentialPrompts = false; rv.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;

/*- Configure ServerReport -*/ /* The SomeCustomConfigurationObject retrieves configuration info from the web.config file */ var config = new SomeCustomConfigurationObject(); if (!config.DefaultCredentials) rpt.ReportServerCredentials = new ReportServerCredentials(config);

rpt.ReportServerUrl = config.ReportServerUrl; rpt.ReportPath = config.RootPathWithSlashes + reportName;

/*- Set LoginName Parameter (?) -*/ p = rpt.GetParameters()"LoginName";

if (p != null) { var loginName = AppUserManager.CurrentUserName; var p2 = new ReportParameter("LoginName", loginName); rpt.SetParameters(p2); }

/*- Set SelectedTenantID Parameter (?) -*/ p = rpt.GetParameters()"SelectedTenantID";

if (p != null) { var tid = ControllerHelpers.GetSelectedTenantID(HttpContext.Current.Request); var p2 = new ReportParameter("SelectedTenantID", tid.ToString()); rpt.SetParameters(p2); }

rpt.Refresh(); } catch (Exception ex) { new EventLogManager().LogException(ex); System.Diagnostics.Debug.Print(ex.Message); throw; } }

}@@

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.