locked
ReportViewer LocalReport LoadSubreportDefinition fails RRS feed

  • Question

  • I'm trying to use the Winform ReportViewer (in local mode) and want to load a report and subreport using the LocalReport.LoadReportDefinition and LocalReport.LoadSubreportDefinition methods. However, when I run the report in my ReportViewer, I get a message within the report (where the subreport should be) stating that the subreport could not be found at the specified location (the location being the value in the ReportName field for the subreport). So it appears that the ReportViewer is looking for the subreport using the name supplied in the subreport ReportName field rather than the stream supplied to the LoadSubreportDefinition method. But if I leave the subreport's ReportName field empty (trying to make that field ignored), then when I run the report, I get a message (in the ReportViewer) that a ReportName cannot be an empty string or just white space.

    So if I use LoadReportDefinition, I have to use LoadSubreportDefinition for subreports (according to documentation). But LoadSubreportDefinition fails because ReportName is used for loading subreports. But I can't leave ReportName blank (to have it be ignored). So it appears LoadReportDefinition can't be used. Is this a defect in the current release - or is some trick to making this work?

    Tuesday, April 6, 2010 5:41 PM

Answers

  • Ok, I see what I was doing wrong.

     I needed to supply LoadSubreportDefinition the value of the ReportName field for the subreport within the main report. I had been (wrongly) using the Name field for the subreport instead.

    The sample code below worked for me. For this example, ReportName = "ReportNameValue" for the subreport within my main report.

    void ShowReportInViewer()
    {
        this.MainReportViewer.Reset();
    
        StreamReader report = File.OpenText(@"Reports\MyMainReport.rdlc");
        this.MainReportViewer.LocalReport.LoadReportDefinition(report);
    
        StreamReader subReport = File.OpenText(@"Reports\MySubReport.rdlc");
        this.MainReportViewer.LocalReport.LoadSubreportDefinition("ReportNameValue", subReport);
    
        this.MainReportViewer.RefreshReport();
    }
    

     

    • Marked as answer by Aland Li Thursday, April 8, 2010 11:35 AM
    Thursday, April 8, 2010 2:07 AM
  • Here is some code that I've used before to load reports with sub-reports and had much success. The sub-report name is the name by which the sub-report will be referenced when used by the parent report.

    private void RenderExportsPerUser(LocalReport localReport)
    {
        List<ReportParameter> parameters = new List<ReportParameter>();
        parameters.Add(new ReportParameter("baseReportUrl", mBaseUrl));
    
        Dictionary<Guid, MembershipUser> reportUsers = new Dictionary<Guid, MembershipUser>();
        MembershipUserCollection users = CommonMethods.UsersGetAll();
        List<Repository> repos = CommonMethods.ExportRepositoriesGet(null);
        foreach (Repository repo in repos)
        {
            IEnumerable<ExportLabel> labels = repo.GetExportLabels();
            foreach (ExportLabel label in labels)
            {
                Repository repoDetail = CommonMethods.ExportDetailsGetByLabelId(null, label.ExportLabelId);
                IEnumerable<ExportRequestData> requests = repoDetail.ExportLabels[0].GetExportRequests();
                foreach (ExportRequestData request in requests)
                {
                    mExportRequests.Add(request);
                    if (!reportUsers.ContainsKey(request.UserId))
                    {
                        foreach (MembershipUser user in users)
                        {
                            if ((Guid)user.ProviderUserKey == request.UserId)
                            {
                                reportUsers.Add(request.UserId, user);
                                break;
                            }
                        }
                    }
                }
            }
        }
    
        Stream reportDef = GetReportDefinition("UserExports.rdlc");
        Stream subReportDef = GetReportDefinition("UserExportsDetails.rdlc");
    
        localReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
    
        localReport.LoadReportDefinition(reportDef);
        localReport.LoadSubreportDefinition("UserExportsDetails", subReportDef);
    
        localReport.DataSources.Clear();
        localReport.DataSources.Add(new ReportDataSource("Users", reportUsers.Values));
    
        localReport.SetParameters(parameters);
        localReport.DisplayName = "Exports per User";
        localReport.EnableHyperlinks = true;
    }

    "There's a way to do it better - find it." - Thomas Edison
    • Marked as answer by Aland Li Thursday, April 8, 2010 11:35 AM
    Tuesday, April 6, 2010 9:11 PM

All replies

  • Here is some code that I've used before to load reports with sub-reports and had much success. The sub-report name is the name by which the sub-report will be referenced when used by the parent report.

    private void RenderExportsPerUser(LocalReport localReport)
    {
        List<ReportParameter> parameters = new List<ReportParameter>();
        parameters.Add(new ReportParameter("baseReportUrl", mBaseUrl));
    
        Dictionary<Guid, MembershipUser> reportUsers = new Dictionary<Guid, MembershipUser>();
        MembershipUserCollection users = CommonMethods.UsersGetAll();
        List<Repository> repos = CommonMethods.ExportRepositoriesGet(null);
        foreach (Repository repo in repos)
        {
            IEnumerable<ExportLabel> labels = repo.GetExportLabels();
            foreach (ExportLabel label in labels)
            {
                Repository repoDetail = CommonMethods.ExportDetailsGetByLabelId(null, label.ExportLabelId);
                IEnumerable<ExportRequestData> requests = repoDetail.ExportLabels[0].GetExportRequests();
                foreach (ExportRequestData request in requests)
                {
                    mExportRequests.Add(request);
                    if (!reportUsers.ContainsKey(request.UserId))
                    {
                        foreach (MembershipUser user in users)
                        {
                            if ((Guid)user.ProviderUserKey == request.UserId)
                            {
                                reportUsers.Add(request.UserId, user);
                                break;
                            }
                        }
                    }
                }
            }
        }
    
        Stream reportDef = GetReportDefinition("UserExports.rdlc");
        Stream subReportDef = GetReportDefinition("UserExportsDetails.rdlc");
    
        localReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing);
    
        localReport.LoadReportDefinition(reportDef);
        localReport.LoadSubreportDefinition("UserExportsDetails", subReportDef);
    
        localReport.DataSources.Clear();
        localReport.DataSources.Add(new ReportDataSource("Users", reportUsers.Values));
    
        localReport.SetParameters(parameters);
        localReport.DisplayName = "Exports per User";
        localReport.EnableHyperlinks = true;
    }

    "There's a way to do it better - find it." - Thomas Edison
    • Marked as answer by Aland Li Thursday, April 8, 2010 11:35 AM
    Tuesday, April 6, 2010 9:11 PM
  • Ok, I see what I was doing wrong.

     I needed to supply LoadSubreportDefinition the value of the ReportName field for the subreport within the main report. I had been (wrongly) using the Name field for the subreport instead.

    The sample code below worked for me. For this example, ReportName = "ReportNameValue" for the subreport within my main report.

    void ShowReportInViewer()
    {
        this.MainReportViewer.Reset();
    
        StreamReader report = File.OpenText(@"Reports\MyMainReport.rdlc");
        this.MainReportViewer.LocalReport.LoadReportDefinition(report);
    
        StreamReader subReport = File.OpenText(@"Reports\MySubReport.rdlc");
        this.MainReportViewer.LocalReport.LoadSubreportDefinition("ReportNameValue", subReport);
    
        this.MainReportViewer.RefreshReport();
    }
    

     

    • Marked as answer by Aland Li Thursday, April 8, 2010 11:35 AM
    Thursday, April 8, 2010 2:07 AM