In the last issue, I introduced you to the basics of incorporating SQL Server Reporting Services into your ASP.NET MVC applications. In this issue, I’ll finish the series by illustrating how we can transfer data between the ASP.NET MVC context and the SSRS report context. In addition, I will also cover deployment issues such as authentication.

To illustrate these additional concepts, I will use our trusty old friend, the Northwind database. In particular, the report will leverage the Alphabetical List of Products view.Figure 1 illustrates the design time view of our report. The report has one parameter - category. Ultimately, this parameter will be supplied by the ASP.NET MVC application.Figure 2 illustrates the report in preview mode, listing products in the condiment category. In part 1 of this article I covered how to build and deploy reports to the report server. If you need to primer on those concepts, please consult that previous article.

Figure 1: The report in this example is a listing of products, sorted by category.Figure 2: Product listing report in preview mode.

Passing Data Between Contexts

Listing 1 illustrates the controller method that launches the product listing report.Figure 3 illustrates the ASP.NET MVC page that invokes the ProductListingReport action. The key to passing data between contexts lies in the Session variable. The code uses a hash to keep track of report variables and their respective values. That hash is stored in the Session[“reportParameters”] variable. In part 1 of this article, the process was very specific in regards to the generated report. In fact, the Web Form that hosts the report viewer control could run any report. In other words, the report viewer controller only needs to know the report server and the report name. Accordingly, the report server and report name details will be passed to the report viewer control. With these examples we very easily achieve reusability. The last line of code re-directs the application to the report viewer aspx page. Listing 2 illustrates how the report viewer acts on these passed values.

In Listing 2, there are four basic operations:

Figure 3: To run the report, the user selects a product category and clicks the Run Report button.

  • Report server specification
  • Report name specification
  • Security credentials specification
  • Report parameter setting

You may be wondering where the report server and security credential information is stored. Because the report server and security credentials are not specific to any one report, that data should not be specified in a controller. Rather, these data points belong as part of the application configuration. In the case of an ASP.NET application, those configuration items are stored in the web.config file. The following configuration items supply the needed data:

  1. <appSettings>
  2. <add key="ReportServerURL"
  3. value="http://host/reportserver" />
  4. <add key ="ReportServerDomain" value="Domain"/>
  5. <add key ="ReportServerUser" value="User"/>
  6. <add key="ReportServerPassword"
  7. value="Password"/>
  8. </appSettings>

The domain, user and password data listed is for illustration purposes only. In a real production scenario, the report server will exist on a separate server that has its own authentication requirements. In order gain access to and to run those reports, authentication credentials must be passed to the report viewer.

Generating the Report

Because the ASP.NET MVC app will supply the necessary parameter values, there is no need to make parameters accessible or visible within the report. Figure 4 illustrates the report as generated by the ASP.NET MVC application. The following snippet shows the SQL code used by the report. The code incorporates the @category parameter. That is how the results are filtered.

  1. SELECT *
  2. FROM
  3. [Northwind].[dbo].[Alphabetical list of products]
  4. where CategoryName like @category

Conclusion

The combination of ASP.NET MVC and SQL Server Reporting Services (SSRS) provide a framework for robust applications. In this two-part series, you have seen how easy it is to extend your ASP.NET MVC applications with SSRS. With the approach presented here, you can easily achieve reusability, thereby using a common set of application components that are not specific to any one report and/or report server. I hope you find the information presented over these past two issues to be useful in your application development efforts. Until next time - happy coding!

  1. Listing 1: Controller method to launch report
  2. [HttpPost]
  3. public ActionResult ProductListingReport(FormCollection form)
  4. {
  5. var reportParameters =
  6. new Dictionary<string, string>();
  7.  
  8. if (form["Category"] == "All")
  9. form["Category"] = "%";
  10.  
  11. reportParameters.Add("category",
  12. form["Category"]);
  13.  
  14. Session["reportParameters"] =
  15. reportParameters;
  16. Session["reportPath"] =
  17. "/ASPMVCReports/ProductCategoryListing";
  18. return
  19. Redirect("../Reports/ReportViewer.aspx");
  20. }
  21. Listing 2: Page_Load code for ReportViewer.aspx page
  22. protected void Page_Load(object sender, EventArgs e)
  23. {
  24. if (!Page.IsPostBack)
  25. {
  26.  
  27. //Specify the report server
  28. ReportViewer1.
  29. ServerReport.
  30. ReportServerUrl =
  31. new Uri(WebConfigurationManager.
  32. AppSettings["ReportServerURL"]);
  33.  
  34. //Specify the report name
  35. ReportViewer1.
  36. ServerReport.
  37. ReportPath = Session["reportPath"].ToString();
  38.  
  39. //Specify the server credentials
  40. ReportViewer1.
  41. ServerReport.
  42. ReportServerCredentials =
  43. new CustomReportCredentials
  44. (
  45. WebConfigurationManager.
  46. AppSettings["ReportServerUser"],
  47. WebConfigurationManager.
  48. AppSettings["ReportServerPassword"],
  49. WebConfigurationManager.
  50. AppSettings["ReportServerDomain"]
  51. );
  52. /*
  53. * With the report specified, hydrate the report
  54. * parameters based on the values in the
  55. * reportParameters hash.
  56. */
  57. var reportParameters = (Dictionary<string,
  58. string>)Session["reportParameters"];
  59.  
  60. foreach (var item in reportParameters)
  61. {
  62. ReportViewer1.
  63. ServerReport.
  64. SetParameters(
  65. new List<ReportParameter>()
  66. {
  67. new ReportParameter
  68. (item.Key, item.Value)
  69. });
  70. }
  71. }
  72. }

  

 

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

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

    Your ASP.NET MVC application needs reports. What do you do? In this article, I will demonstrate how ...

  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. dockerFile 配置puppeteer

    ## install npm && puppeteer## 必要依赖 libXScrnSaver RUN yum -y install libXScrnSaver ## install ...

  2. 我的常用的Linux命令

    环境:centos7 主要应用Linux命令是为了搭建环境,所以记录一下我的常用的Liunx命令 一.常用目录.文件操作命令 1.显示目录列表命令 ls       显示当前目录下的可见文件 ls - ...

  3. linux下升级php5.4到php5.6

    进入终端后查看php版本 php -v 输出可能如下: PHP 5.4.35 (cli) (built: Nov 14 2014 07:04:10) Copyright (c) 1997-2014 T ...

  4. 安装gradle和配置

    1:官网下载地址:https://docs.gradle.org/current/userguide/installation.html 下载自己认为的版本(压缩包) 2:解压到目标目录 3:配置gr ...

  5. 使用xmake优雅地描述工程

    描述语法 xmake的描述语法基于lua实现,因此描述语法继承了lua的灵活性和简洁性,并且通过28原则,将描述作用域(简单描述).脚本作用域(复杂描述)进行分离,使得工程更加的简洁直观,可读性非常好 ...

  6. data plugin for vs2019

    Reporting Service projects for VS 2019https://marketplace.visualstudio.com/items?itemName=ProBITools ...

  7. IDEA中Java目录结构

    IDEA中Java的目录结构 1.首先新建Project,选择Empty,新建空的项目 2.选择Module时候,需要选择JDK,JDK只需要选择到Java Home目录就可以了 3.创建好Modul ...

  8. SCUT - 153 - 小马哥和他的山脉 - 线段树

    https://scut.online/p/153 其实不需要用线段树,只关心相邻元素的差,像神仙那样用差分就可以O1维护的. 但是我偏要用. 交之前写的那个,注意没有st本身的线段树只有lazy标记 ...

  9. asp.net 获取表单中控件的值

    原文:https://blog.csdn.net/happymagic/article/details/8480235   C# 后台获取前台 input 文本框值.(都是以控件的Name来获取) s ...

  10. ionic -v2版本项目结构

    myApp │  config.xml  //项目配置文件,包名.名称.minSdkVersion等都在此处配置 │  ionic.config.json │  package.json  //项目依 ...