开发者

ReportViewer getting null error with subreport

开发者 https://www.devze.com 2023-03-26 21:47 出处:网络
I\'m building a small application in Visual Studio 2010 with .Net 4.0 and C# where I\'m generating a ReportViewer-report from a List. I then have a subreport in my tablix that should pass a property-v

I'm building a small application in Visual Studio 2010 with .Net 4.0 and C# where I'm generating a ReportViewer-report from a List. I then have a subreport in my tablix that should pass a property-value from WebLink called ProviderIdentifier. I implement the SubReportProcessing-event on my report to return data to the subreport like this:

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

Currently I return the same links for all instances of the subreport. The report works fine until I try to pass a parameter to the subreport. When I add the parameter using ProviderIdentifier (which I can display in my report without problem) I always get a NullReferenceException with the message "Object reference not set to an instance of an object." when I call Render() on my LocalReport-object. The same happens if I add a static value (like 1) to the report instead of passing ProviderIdentifier. If I remove the parameter all together it works great though but I have no way to identify which links to return to the subreport.

Anyone know what could cause this problem?

Complete code:

public void RenderReport(开发者_Go百科)
{
    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);
}


It took a few hours but I finally found what I had missed. If you check the properties on a report you can set "Variables" there which I've tested so many ways to create a match for the parameter coming from the main report. What I had totally missed (and couldn't find described properly on the net) was that in the treeview on your right in the editor you have a folder called "Parameters". I added a parameter there that corresponds to the one my main report is passing and now it works as it should!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号