为什么数据检索因 Microsoft ReportViewer 的子报表错误而失败?
Why data retrieval failed with subreport Error with Microsoft ReportViewer?
我在处理报告时遇到问题,但奇怪的是它只在生产环境中不起作用,如果我在本地 IIS 上部署我的解决方案或在 VS2013 调试阶段我可以看到正确填充子报告的报告.
我正在使用 VS2013 和 ReportViewer2012。
因此,在开发和测试环境中一切正常,但在生产环境中,当我调用打印时出现此 "Data retrieval failed for the subreport ... (for all subreports) "。
为什么?
所以,我有一个报表容器,里面有一些子报表,这里有一些代码:
来源
loadDataSources(); //loading all datatables of subreports
ReportViewer ReportViewer1 = new ReportViewer();
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.Visible = false;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/ReportContainer.rdlc");
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet0", _datatableContainer));
ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
CreatePDF(ReportViewer1, uniquefilename);
void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
if (e.ReportPath.ToLower() == "rpt_sub_1")
{
e.DataSources.Add(new ReportDataSource("DataSet1", _datatable1));
e.DataSources.Add(new ReportDataSource("DataSet2", _datatable2));
e.DataSources.Add(new ReportDataSource("DataSet3", _datatable3));
}
if (e.ReportPath.ToLower() == "rpt_sub_2")
{
e.DataSources.Add(new ReportDataSource("DataSet4", _datatable4));
e.DataSources.Add(new ReportDataSource("DataSet5", _datatable5));
e.DataSources.Add(new ReportDataSource("DataSet6", _datatable6));
e.DataSources.Add(new ReportDataSource("DataSet7", _datatable7));
}
//...
非常感谢
好的,我解决了这个问题。
问题是值 e.ReportPath。
在测试和开发环境中是这样的:
"rpt_sub_1" 或 "rpt_sub_2"
但在生产环境中是不同的:
"C:\inetpub\wwwroot\MyWebSite\rpt_sub_1.rdlc" 和 "C:\inetpub\wwwroot\MyWebSite\rpt_sub_2.rdlc"
我修复了创建扩展方法的问题:
public static string CleanerReportName(this string value)
{
value = value.ToLower();
List<string> _listRemoveItems = new List<string>();
_listRemoveItems.Add(@"C:\inetpub\wwwroot\MyWebSite\");
_listRemoveItems.Add(".rdlc");
_listRemoveItems.ForEach(x => value = value.Replace(x, ""));
return value;
}
然后我修改了源代码如下:
void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
if (e.ReportPath.CleanerReportName() == "rpt_sub_1")
{
e.DataSources.Add(new ReportDataSource("DataSet1", _datatable1));
e.DataSources.Add(new ReportDataSource("DataSet2", _datatable2));
e.DataSources.Add(new ReportDataSource("DataSet3", _datatable3));
}
if (e.ReportPath.CleanerReportName() == "rpt_sub_2")
{
e.DataSources.Add(new ReportDataSource("DataSet4", _datatable4));
e.DataSources.Add(new ReportDataSource("DataSet5", _datatable5));
e.DataSources.Add(new ReportDataSource("DataSet6", _datatable6));
e.DataSources.Add(new ReportDataSource("DataSet7", _datatable7));
}
//...
我希望这个 post 能对以后的人有所帮助。
再见
我在处理报告时遇到问题,但奇怪的是它只在生产环境中不起作用,如果我在本地 IIS 上部署我的解决方案或在 VS2013 调试阶段我可以看到正确填充子报告的报告.
我正在使用 VS2013 和 ReportViewer2012。
因此,在开发和测试环境中一切正常,但在生产环境中,当我调用打印时出现此 "Data retrieval failed for the subreport ... (for all subreports) "。 为什么?
所以,我有一个报表容器,里面有一些子报表,这里有一些代码:
来源
loadDataSources(); //loading all datatables of subreports
ReportViewer ReportViewer1 = new ReportViewer();
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.Visible = false;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/ReportContainer.rdlc");
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet0", _datatableContainer));
ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
CreatePDF(ReportViewer1, uniquefilename);
void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
if (e.ReportPath.ToLower() == "rpt_sub_1")
{
e.DataSources.Add(new ReportDataSource("DataSet1", _datatable1));
e.DataSources.Add(new ReportDataSource("DataSet2", _datatable2));
e.DataSources.Add(new ReportDataSource("DataSet3", _datatable3));
}
if (e.ReportPath.ToLower() == "rpt_sub_2")
{
e.DataSources.Add(new ReportDataSource("DataSet4", _datatable4));
e.DataSources.Add(new ReportDataSource("DataSet5", _datatable5));
e.DataSources.Add(new ReportDataSource("DataSet6", _datatable6));
e.DataSources.Add(new ReportDataSource("DataSet7", _datatable7));
}
//...
非常感谢
好的,我解决了这个问题。
问题是值 e.ReportPath。
在测试和开发环境中是这样的: "rpt_sub_1" 或 "rpt_sub_2"
但在生产环境中是不同的: "C:\inetpub\wwwroot\MyWebSite\rpt_sub_1.rdlc" 和 "C:\inetpub\wwwroot\MyWebSite\rpt_sub_2.rdlc"
我修复了创建扩展方法的问题:
public static string CleanerReportName(this string value)
{
value = value.ToLower();
List<string> _listRemoveItems = new List<string>();
_listRemoveItems.Add(@"C:\inetpub\wwwroot\MyWebSite\");
_listRemoveItems.Add(".rdlc");
_listRemoveItems.ForEach(x => value = value.Replace(x, ""));
return value;
}
然后我修改了源代码如下:
void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
if (e.ReportPath.CleanerReportName() == "rpt_sub_1")
{
e.DataSources.Add(new ReportDataSource("DataSet1", _datatable1));
e.DataSources.Add(new ReportDataSource("DataSet2", _datatable2));
e.DataSources.Add(new ReportDataSource("DataSet3", _datatable3));
}
if (e.ReportPath.CleanerReportName() == "rpt_sub_2")
{
e.DataSources.Add(new ReportDataSource("DataSet4", _datatable4));
e.DataSources.Add(new ReportDataSource("DataSet5", _datatable5));
e.DataSources.Add(new ReportDataSource("DataSet6", _datatable6));
e.DataSources.Add(new ReportDataSource("DataSet7", _datatable7));
}
//...
我希望这个 post 能对以后的人有所帮助。 再见