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

时间:2021-08-17 01:19:08

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.

Incorporating ASP.NET MVC and SQL Server Reporting Services, Part 2Figure 1: The report in this example is a listing of products, sorted by category.Incorporating ASP.NET MVC and SQL Server Reporting Services, Part 2Figure 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:

Incorporating ASP.NET MVC and SQL Server Reporting Services, Part 2Figure 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:

<appSettings>
<add key="ReportServerURL"
value="http://host/reportserver" />
<add key ="ReportServerDomain" value="Domain"/>
<add key ="ReportServerUser" value="User"/>
<add key="ReportServerPassword"
value="Password"/>
</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.

SELECT *
FROM
[Northwind].[dbo].[Alphabetical list of products]
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!

Listing 1: Controller method to launch report
[HttpPost]
public ActionResult ProductListingReport(FormCollection form)
{
var reportParameters =
new Dictionary<string, string>(); if (form["Category"] == "All")
form["Category"] = "%"; reportParameters.Add("category",
form["Category"]); Session["reportParameters"] =
reportParameters;
Session["reportPath"] =
"/ASPMVCReports/ProductCategoryListing";
return
Redirect("../Reports/ReportViewer.aspx");
}
Listing 2: Page_Load code for ReportViewer.aspx page
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{ //Specify the report server
ReportViewer1.
ServerReport.
ReportServerUrl =
new Uri(WebConfigurationManager.
AppSettings["ReportServerURL"]); //Specify the report name
ReportViewer1.
ServerReport.
ReportPath = Session["reportPath"].ToString(); //Specify the server credentials
ReportViewer1.
ServerReport.
ReportServerCredentials =
new CustomReportCredentials
(
WebConfigurationManager.
AppSettings["ReportServerUser"],
WebConfigurationManager.
AppSettings["ReportServerPassword"],
WebConfigurationManager.
AppSettings["ReportServerDomain"]
);
/*
* With the report specified, hydrate the report
* parameters based on the values in the
* reportParameters hash.
*/
var reportParameters = (Dictionary<string,
string>)Session["reportParameters"]; foreach (var item in reportParameters)
{
ReportViewer1.
ServerReport.
SetParameters(
new List<ReportParameter>()
{
new ReportParameter
(item.Key, item.Value)
});
}
}
}

  

 

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

  1. Incorporating ASP&period;NET MVC and SQL Server Reporting Services&comma; Part 1

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

  2. ASP&period;NET MVC与Sql Server交互&comma;把字典数据插入数据库

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

  3. ASP&period;NET MVC与Sql Server交互&comma; 插入数据

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

  4. &lbrack;转&rsqb;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&period;

    这次使用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 &lpar;SQLEXPRESS&rpar; 服务占用80端口

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

随机推荐

  1. How Spring Boot Autoconfiguration Magic Works--转

    原文地址:https://dzone.com/articles/how-springboot-autoconfiguration-magic-works In my previous post &qu ...

  2. linux expect

    1.首先确定是否安装expect /home/root> which expect /usr/bin/expect 如果没有安装,先安装一下 安装方法: 请参考 http://www.cnblo ...

  3. I7-5775C之所以被Intel跳过,是因为本身有太多BUG

    说起I7-5775C,第五代酷睿处理器,可能大多数人都没有使用过,也并不清楚他有什么样的特性. 在2015年6月份,我在日本亚马逊买了一个I7-5775C,从此噩梦就开始了(现在已经换了I7-5820 ...

  4. Ubuntu12&period;04配置mod&lowbar;python

    安装: sudo apt-get install libapache2-mod-python python-mysqldb 然后编辑配置文件/etc/apache2/sites-enabled/000 ...

  5. 【英语】Bingo口语笔记&lpar;43&rpar; - u长短音

  6. cdh集群ip更改

    #---1.修改每个用户的hosts vi /etc/hosts #127.0.0.1 localhost localhost.localdomain localhost4 localhost4.lo ...

  7. min-max容斥笔记及例题

    这个东西是一个非常好玩的数学工具. $$max(S)=\sum_{T\subset S}(-1)^{|T|-1}min(T)$$ $$max_k(S)=\sum_{T\subset S}(-1)^{| ...

  8. Improving your submission -- Kaggle Competitions

    1: Improving Our Features In the last mission, we made our first submission to Titanic: Machine Lear ...

  9. WARNING&colon; Package of target &lbrack;javax&period;servlet&period;jsp&period;jstl&period;core&period;LoopTagSupport&dollar;1Status&commat;7439e436&rsqb; or package of member &lbrack;public int javax&period;servlet&period;jsp&period;jstl&period;core&period;LoopTagSupport&dollar;1Status&period;getIndex&lpar;&rpar;&rsqb; are excluded&excl;

    Struts2爆出045漏洞后,将struts版本升级到了2.3.32.但是在验证时发现有些jstl循环未出现预期的结果. debug发现,数据没有问题,断定是前端页面显示出了问题.根据日志信息WAR ...

  10. HashMap中的hash函数

    在写一个HashSet时候有个需求,是判断HashSet中是否已经存在对象,存在则取出,不存在则add添加.HashSet也是通过HashMap实现,只用了HashMap的key,value都存储一个 ...