ReportViewer使用子报表获取null错误

时间:2011-08-08 12:17:00

标签: c# .net reportviewer rdlc subreport

我正在使用.Net 4.0和C#在Visual Studio 2010中构建一个小应用程序,我正在从List生成ReportViewer报告。然后我在tablix中有一个子报表,它应该从WebLink传递一个名为ProviderIdentifier的属性值。我在报表上实现SubReportProcessing事件,将数据返回到子报表,如下所示:

private void localReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
{
    List<WebLink> links = LinkProvider.FetchSublinks(LinkProvider.Fetch(new WebLink(new Uri("http://www.contentstudio.se/"))));
    e.DataSources.Add(new ReportDataSource("ParentLinks", links));
}

目前,我为子报表的所有实例返回相同的链接。该报告正常工作,直到我尝试将参数传递给子报表。当我使用ProviderIdentifier添加参数时(我可以在我的报告中显示没有问题)我总是得到一个NullReferenceException,消息“对象引用未设置为对象的实例”。当我在我的LocalReport对象上调用Render()时。如果我向报表添加静态值(如1)而不是传递ProviderIdentifier,则会发生同样的情况。如果我将所有参数一起删除它虽然工作得很好,但我无法识别返回子报表的链接。

任何人都知道会导致这个问题的原因吗?

完整代码:

public void RenderReport()
{
    LocalReport localReport = new LocalReport
                                    {
                                        ReportPath = ("BrokenLink.rdlc"),
                                        EnableHyperlinks = true,
                                        ShowDetailedSubreportMessages = true
                                    };

    List<WebLink> links = LinkProvider.FetchSublinks(LinkProvider.Fetch(new WebLink(new Uri("http://www.contentstudio.se/"))));

    ReportDataSource reportDataSource = new ReportDataSource("Weblinks", links);
    localReport.DataSources.Add(reportDataSource);

    localReport.SubreportProcessing += localReport_SubreportProcessing;

    const string reportType = "PDF";
    string mimeType;
    string encoding;
    string fileNameExtension;

    //The DeviceInfo settings should be changed based on the reportType
    //http://msdn2.microsoft.com/en-us/library/ms155397.aspx
    const string deviceInfo = "<DeviceInfo>" +
                                "  <OutputFormat>PDF</OutputFormat>" +
                                "  <PageWidth>8.5in</PageWidth>" +
                                "  <PageHeight>11in</PageHeight>" +
                                "  <MarginTop>0.5in</MarginTop>" +
                                "  <MarginLeft>1in</MarginLeft>" +
                                "  <MarginRight>1in</MarginRight>" +
                                "  <MarginBottom>0.5in</MarginBottom>" +
                                "  <DpiX>72</DpiX>" +
                                "  <DpiY>72</DpiY>" +
                                "</DeviceInfo>";
    Warning[] warnings;
    string[] streams;

    //Render the report
    byte[] renderedBytes = localReport.Render(
        reportType,
        deviceInfo,
        out mimeType,
        out encoding,
        out fileNameExtension,
        out streams,
        out warnings);

    File.WriteAllBytes("I:\\report.pdf", renderedBytes);
}

1 个答案:

答案 0 :(得分:4)

花了几个小时但我终于找到了我错过的东西。如果检查报告上的属性,可以设置“变量”,我已经测试了很多方法来为主报告中的参数创建匹配。我完全错过了(并且无法在网上找到正确描述)的是,在编辑器右侧的树视图中,您有一个名为“参数”的文件夹。我在那里添加了一个参数,该参数对应于我的主报告正在传递的参数,现在它可以正常工作!