SSRS“LoadSubreportDefinition”获取错误“尚未指定运行报告所需的一个或多个参数”

时间:2017-05-29 08:54:28

标签: c# asp.net visual-studio visual-studio-2013 reporting-services

我使用Visual Studio 2013创建了一个.rdlc报告,我们将其称为Main.rdlc,并在其中包含子报告控件。

另一方面,我创建了一个Sub.rdlc作为与Main.rdlc链接的子报表

我使用以下代码在后面的代码上设置子报表路径:

ReportViewer viewer = new ReportViewer();

// load report definition
string reportTemplate = Server.MapPath("~/Main.rdlc");
viewer.LocalReport.LoadReportDefinition(ReportLocalizator.Localize(reportTemplate, culture));
viewer.LocalReport.EnableHyperlinks = true; 
viewer.LocalReport.SetParameters(new ReportParameter("Culture", culture));

// load subreport definition
viewer.LocalReport.SubreportProcessing += LocalReport_SubreportProcessing;
string subReportTemplate = Server.MapPath("~/Sub.rdlc");
viewer.LocalReport.LoadSubreportDefinition("SubReport", ReportLocalizator.Localize(subReportTemplate, culture));

viewer.LocalReport.DataSources.Clear();
viewer.LocalReport.DataSources.Add(new ReportDataSource("MyDataSet", myDataSet.Data.DefaultView));
viewer.LocalReport.EnableHyperlinks = true;
viewer.LocalReport.Refresh();

Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;

byte[] bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
Response.ClearHeaders();
Response.BufferOutput = true;
Response.Buffer = true;
Response.HeaderEncoding = System.Text.Encoding.UTF8;
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-length", bytes.Length.ToString());
Response.AddHeader("content-disposition", "attachment; filename=PrintedForm" + "." + extension);
Response.OutputStream.Write(bytes, 0, bytes.Length); // create the file  
Response.Flush(); // send it to the client to download  
Response.End();

但是,我总是收到错误“尚未指定运行报告所需的一个或多个参数。”尽管子报表中没有参数。

删除此部分时代码运行良好

// load subreport definition
viewer.LocalReport.SubreportProcessing += LocalReport_SubreportProcessing;
string subReportTemplate = Server.MapPath("~/Sub.rdlc");
viewer.LocalReport.LoadSubreportDefinition("SubReport", ReportLocalizator.Localize(subReportTemplate, culture));

是导致错误的代码还是rdlc本身?有没有人有这个错误的经验?任何形式的帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

问题在于代码。错误告诉主报告中缺少参数。

这是因为我在SetParameters电话之前拨打了LoadSubReportDefinition

我不确定原因,但是调用LoadSubReportDefinition似乎清除了之前设置的所有参数。

因此解决方案是在SetParameters

之后移动LoadSubReportDefinition来电
ReportViewer viewer = new ReportViewer();

// load report definition
string reportTemplate = Server.MapPath("~/Main.rdlc");
viewer.LocalReport.LoadReportDefinition(ReportLocalizator.Localize(reportTemplate, culture));
viewer.LocalReport.EnableHyperlinks = true; 

// The problem is here! Move it after LoadSubreportDefinition
// viewer.LocalReport.SetParameters(new ReportParameter("Culture", culture));

// load subreport definition
viewer.LocalReport.SubreportProcessing += LocalReport_SubreportProcessing;
string subReportTemplate = Server.MapPath("~/Sub.rdlc");
viewer.LocalReport.LoadSubreportDefinition("SubReport", ReportLocalizator.Localize(subReportTemplate, culture));

// Move it here!
viewer.LocalReport.SetParameters(new ReportParameter("Culture", culture));

viewer.LocalReport.DataSources.Clear();
viewer.LocalReport.DataSources.Add(new ReportDataSource("MyDataSet", myDataSet.Data.DefaultView));
viewer.LocalReport.EnableHyperlinks = true;
viewer.LocalReport.Refresh();