2

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

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);
}
4

1 回答 1

4

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

于 2011-08-08T20:25:10.820 回答