Your ASP.NET MVC application needs reports. What do you do? In this article, I will demonstrate how simple it is to weave SQL Server Reporting Services (SSRS) into your ASP.NET MVC Applications.

Just about every application deals with data in one form or another. It also seems that no application is complete unless it contains at least one report. In ASP.NET MVC, the most basic option is to “roll your own” reporting solution. That is certainly feasible but it is not as preferable as using a tool that is dedicated to a specific task, reporting in this case. In other words, why reinvent the wheel?

Instead of going into extensive detail on the specifics of ASP.NET MVC and SSRS, this article will focus on integrating SSRS into ASP.NET MVC. Even if you are not familiar with these technologies, this article will give you a clear understanding of the power of each of these applications in their own right and when combined. For a more extensive discussion on the details of SSRS and ASP.NET MVC, please consult the CODE Magazinearchives.

In order to apply the concepts in this article, you will need SQL Server 2008 with Reporting Services installed. In addition, you will need either Visual Studio 2008 or 2010 with either ASP.NET MVC 1.0 or 2.0 installed. The code examples illustrated herein were created with Visual Studio 2008 and ASP.NET MVC 2.0. The example report displays table and column metadata from the Northwind Traders sample database. If you don’t have the Northwind database, don’t worry. Any database will work because the same metadata structures apply to all SQL Server databases. It is also assumed that you will implement these examples locally under a login that has administrator privileges. This will avoid security issues that would normally be encountered in production where the solution is distributed across multiple servers and hardware. Don’t worry though. In Part 2, I will tackle those issues!

The roadmap for this example is very simple. First, each component will be developed separately. Second, the SSRS and ASP.NET MVC components will be brought together to form a consolidated solution. In Part 2, I will expand upon the concepts presented in Part 1 and I will discuss how to pass parameter values from the ASP.NET MVC environment to SSRS. In addition, I will review important issues that may arise when deploying from a development/test environment to a production IIS Server.

The SSRS Component

The example report will use one dataset based on the SQL Query in Listing 1.

Listing 1 shows a very simple query that uses INFORMATION_SCHEMA to return a list of tables, columns and associated data types for the current database. Figure 1 and Figure 2, respectively, illustrate the data source and data set required to drive the report.Figure 3 illustrates the design session for the sample report and Figure 4 illustrates the report in preview mode. As previously mentioned, if you don’t have the Northwind Traders database available, you can use any database you have installed. The SQL query will work with any database. Just set the data source properties appropriately so that the correct database is selected.

Figure 1: The SSRS data source is used by the report to connect to the database.Figure 2: A SSRS data set contains data displayed by the SSRS report.Figure 3: The SSRS design surface.Figure 4: The SSRS report in Preview mode.

ASP.NET MVC views are Web Forms - minus the support for view state.

The only remaining task is to deploy the report. Since you have SQL Server installed locally, go ahead and use the local report server: http://localhost/reportserver. Before you can deploy the report, you need to tell the Reporting Services which server to deploy the report to. To do this, you must maintain the TargetServerURL property in the Project Properties dialog box illustrated in Figure 5. Once you have specified the Server URL, you can deploy the report and data source. To do that, simply right-click the main project node in the Solution Explorer and then select the Deploy menu option. Figure 6illustrates how the newly deployed report appears in the browser.

Figure 5: The SSRS report project Property Page.Figure 6: The SSRS report default browser view.

As far as the SSRS project is concerned, that’s it. The report is deployed and is ready to be used. The next step is to create the base ASP.NET MVC component.

The ASP.NET MVC Component

The initial version of the ASP.NET MVC component is based on the default project template, sans the testing project. There are, however, a few additions required:

  • Controller method to launch report
  • ASP.NET Web Form to host the Report Viewer control

That’s right! In order to pull this solution off, you need to incorporate ASP.NET Web Forms. The good news is that ASP.NET MVC is based on Web Forms. Look at any ASP.NET MVC solution and you will find the System.Web namespace. Therefore, in a very real sense, you aren’t adding anything new to an ASP.NET MVC solution.

With respect to the controller method used to launch the report, technically speaking, even that is not required. However, if at some point you wish to pass information to the SSRS context from the ASP.NET MVC context, then the controller method becomes required. On the other hand, if no such information passing requirement exists, then you are free to just call the report URL itself. Going one step further, the ASP.NET Web Form to host the controller isn’t 100% required. If all you want to do is launch the report, you can simply invoke the URL to launch the report in the browser as I’ve illustrated inFigure 6. However, if you do need to pass information from one context to another, then just as you need the controller method to send the data, you will need the ASP.NET Web Form to receive the data. In order to set the stage for the second part of this article, I will go ahead and incorporate the ASP.NET Web Form.

This brings up an interesting question, if ASP.NET MVC Views are based on Web Forms, then why do you need to include a Web Form? The answer has to do with view state. If you look at the source of an ASP.NET Web Form, you will find a lot of code that is dedicated to maintaining state in a manner similar to that of a Windows Forms application. That, after all, was a main design goal of ASP.NET; to open the world of web development to Windows developers.

With view state comes a potpourri of ASP.NET controls, which, as you might guess, require view state to work. The SSRS Report Viewer is one such control. ASP.NET MVC Views, their ASP.NET Web Form lineage notwithstanding, have no such view state. Consequently, ASP.NET MVC Views cannot use ASP.NET controls that require view state. To some, ASP.NET MVC provides a purer and cleaner web experience like Ruby on Rails. Still, some of those ASP.NET controls are awfully slick! As is often the case, the best solutions come from taking the best of different worlds and mashing them together for a consolidated solution. As you will see, apart from a few differences, ASP.NET MVC Views and ASP.NET Web Forms are not from different worlds and are not all that different.

Advertisement

With the SSRS report in place, let’s turn our attention to integrating the report into the ASP.NET MVC application.

Adding a Web Form to the ASP.NET MVC Application

Starting with the default ASP.NET MVC project, Figure 7 illustrates the newly added ASP.NET Web Form into the Reports folder. So far, it’s just an empty form. From the toolbox, you can easily add the MicrosoftReportViewer control. Figure 8 illustrates the newly added control to the TableListing.aspx page. As you can see, the ReportViewer control looks just like the browser view in Figure 6. Best of all, you get full navigation and export functionality for free!

Figure 7: The TableListing.aspx page is an ASP.NET Web Form that will host the ReportViewer control.Figure 8: The TableListing.aspx page with the MicrosoftReportViewer control.

With the Report Viewer control in place, you only need to perform a few more steps. First, you need to tell the control the location and name of the report. Figure 9illustrates the necessary entries in the ReportViewer1 Properties dialog box. For the ServerReport object property, the following properties have been set:

As is often the case, the best solutions come from taking the best of different worlds and mashing them together for a consolidated solution.

Figure 9: The ReportViewer Properties dialog box.

  • DisplayName: tablelisting
  • ReportPath: /ASPMVCReports/tablelisting
  • ReportServerUrl: http://localhost/reportserver
  • ProcessingMode:Remote

For the second step, you need to specify a controller method for the Home Controller. For this example, the method will be called TableListingReport(). Figure 10 illustrates how simple this new controller method is. For now, the method contains one line of code that re-directs the browser to the report viewer page. Just to close the loop, I’ll had an action link the home view that points to the new controller method. Figure 11illustrates how the home controller’s index view appears. Clicking the Table Listing Report link launches the report as is illustrated in Figure 12.

Figure 10: The TableListingReport() controller method is used to launch the TableListing.aspx web form.Figure 11: The home controller index view with an action link to the new ASP.NET Web Form that hosts the MicrosoftReportViewer control.Figure 12: The Table Listing Report, this time hosted in an ASP.NET Web Form launched from an ASP.NET MVC view.

Conclusion

If you have been wondering when you would ever combine Web Forms in an ASP.NET MVC application, hopefully, this article has helped answer that question. The example illustrated in this article is very simple. Nevertheless, it provides a strong foundation to build upon. Because a controller method was created and because the Report Viewer controller was hosted in a Web Form, the possibility exists to pass data from the ASP.NET MVC context to the SSRS report context. In Part 2 of this article I will demonstrate that concept in detail. In addition, I’ll also cover the deployment issues from the development/test to a production environment. Until the next issue - Happy Coding!!

Listing 1: SQL query to display list of tables and columns in Northwind Traders database
select tables.TABLE_NAME,
columns.ORDINAL_POSITION,
columns.COLUMN_NAME,
case
when columns.data_type like '%char%'
Then columns.data_type +
'('+
convert(varchar(3),columns.character_maximum_length) +
')'
else columns.DATA_TYPE
end as datatype
from INFORMATION_SCHEMA.TABLES tables
join INFORMATION_SCHEMA.COLUMNS columns
on (tables.TABLE_NAME = columns.TABLE_NAME)
where TABLE_TYPE = 'BASE TABLE'
order by tables.TABLE_NAME,
columns.ORDINAL_POSITION

  

Incorporating ASP.NET MVC and SQL Server Reporting Services, Part 1的更多相关文章

  1. Incorporating ASP.NET MVC and SQL Server Reporting Services, Part 2

    In the last issue, I introduced you to the basics of incorporating SQL Server Reporting Services int ...

  2. ASP.NET MVC与Sql Server交互,把字典数据插入数据库

    在"ASP.NET MVC与Sql Server交互, 插入数据"中,在Controller中拼接sql语句.比如: _db.InsertData("insert int ...

  3. ASP.NET MVC与Sql Server交互, 插入数据

    在"ASP.NET MVC与Sql Server建立连接"中,与Sql Server建立了连接.本篇实践向Sql Server中插入数据. 在数据库帮助类中增加插入数据的方法. p ...

  4. [转]SQL Server Reporting Services - Timeout Settings

    本文转自:https://social.technet.microsoft.com/wiki/contents/articles/23508.sql-server-reporting-services ...

  5. SQL Server Reporting Services本机模式下的权限管理

    SQL Server Reporting Services在安装配置后,缺省只给BUILTIN\Administrators用户组(实际上只有本机的Administrator用户)提供管理权限.所以所 ...

  6. SrsDataConnector The SQL Server Reporting Services account is a local user and is not supported.

    这次使用OS+SQL的镜像还原系统后安装了CRM 2015,主要流程是 安装IIS/AD,SSRS ,CRM2015.自带的SQL中SSRS没有安装完全,需配置一下. 这一切都满顺利的,最后在安装 S ...

  7. 充分利用 SQL Server Reporting Services 图表

    最近在查SSRS的一些文章,看到MSDN在有一篇不错的文章,许多图表设置都有说明,共享给大家.. 其中有说明在SSRS中如果去写条件表达写和报表属性中的“自定义代码”,文章相对比较长,需要大家耐心的查 ...

  8. SQL Server Reporting Services – Insufficient Rights Error

    http://www.sql-server-performance.com/2011/security-ssrs-reporting-error/ SQL Server Reporting Servi ...

  9. SQL Server Reporting Services (SQLEXPRESS) 服务占用80端口

    win7, 好多时候,看到system进程占用了80端口,这个是系统进程,不能直接结束.我们不知道这个进程的哪个服务占用了80端口,这里记录其中一个服务"SQL Server Reporti ...

随机推荐

  1. myeclipse如何导入相应的jar包

    方法一: 相应的项目------>右键------->build path-------->Config BuildPath...------->Libraries------ ...

  2. cdoj 15 Kastenlauf dfs

    Kastenlauf Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/3 De ...

  3. JavaScript中事件捕获(Event capturing)-------------->由外向内,事件冒泡(Event bubblin)---------->由内向外

    addEventListener("click", function() {}, useCapture:false); <!DOCTYPE html> <html ...

  4. 关于JS加载的问题

    一些绑定事件之类的行为,如果让他放于外部文件中,或者头部,则会引起所需的内容没有加载出来,找不到Element,导致实现失败.解决这一问题的办法主要采用window.onload事件进行处理,因为在w ...

  5. LINUX O_Direact

    http://laokaddk.blog.51cto.com/368606/699563

  6. ZOJ 2588 Burning Bridges(求含重边的无向连通图的割边) - from lanshui_Yang

    Burning Bridges Time Limit: 5 Seconds Memory Limit: 32768 KB Ferry Kingdom is a nice little country ...

  7. Deepin Linux 安装JDK

    最近在自己的笔记本上安装了Deepin Linux,虽然使用过程中发现一些bugs,但是总体感觉还不错,准备把她作为开发系统使用.系统自带的JDK是Open JDK,但是在做JAVA开发的时候通常需要 ...

  8. LeetCode18 4Sum

    题意: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  9. ASP.NET MVC与WebForm区别

    [转贴一] 使用ASP.NET MVC框架,创建默认项目,第一直观感觉就是地址都是Rewrite过的.对源码和配置文件稍加分析不难看出,MVC使用了httpModules来拦截地址请求,具体用到了Sy ...

  10. MySQL数据库还原:路径必须用正斜杠?

    也是术业不精,其实之前也用命令行还原过几次MySQL数据库,但总记不清语法.这不,今天想把另一台电脑上备份的数据库还原过来,结果不停报错,如下图所示: 后来才发现,因为偷懒直接复制的路径名里,用的全是 ...