c# - ReportViewer 收到子报表 null 错误

标签 c# .net reportviewer rdlc subreport

我正在使用 .Net 4.0 和 C# 在 Visual Studio 2010 中构建一个小型应用程序,在其中从列表生成 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);
}

最佳答案

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

关于c# - ReportViewer 收到子报表 null 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6981963/

相关文章:

c# - 在 C# 中运行并显示富文本文件中的 exe 文件

c# - 旧的 ASP.NET webform 有一些内联代码被编码为 :(

c# - 在 Application 的部分子类中访问 Properties.Resources/Settings

c# - 动态绑定(bind)注入(inject)

Windows 7 64Bit 上的 .NET COM Interop 让我头疼

c# - RLDC 表达式编辑器自定义代码

c# - 在 RDLC 报告中处理数组和聚合

c# - 如何将 SqlBulkCopy 与 SMO 和事务结合使用

c# - 开始一个不需要等待的任务

reporting-services - SSRS 2008 R2 - SSRS 2012 - 报告查看器 : Reports are blank in Safari and Chrome