为什么数据检索失败,子报告错误与Microsoft ReportViewer?

时间:2016-06-24 15:12:46

标签: asp.net-mvc reporting rdlc reportviewer

我的报告有问题,但奇怪的是它只在生产环境中不起作用,如果我在本地IIS上部署我的解决方案或在VS2013的调试阶段,我可以看到正确填充子报告的报告。< / p>

我正在使用VS2013和ReportViewer2012。

因此,在开发和测试环境中一切正常,但在生产时,当我调用打印时,“子报告的数据检索失败...(对于所有子报告)”。 为什么呢?

所以,我有一个Report Container,里面有一些子报告,还有一些代码:

来源

    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));
        }
        //...

非常感谢

1 个答案:

答案 0 :(得分:0)

好的,我解决了这个问题。

问题是值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));
        }
        //...

我希望这篇文章可以帮助将来的某个人。 再见