我正在使用 .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/