Search code examples
asp.net-corerdlcdynamic-rdlc-generation

How do I resolve 'subreport not found' error when using ReportViewerCore.NETCore 15.1.17 with ASP.NET Core rdlc subreport?


When I used it in my code it re-issues the pdf but returns the error in the position subreport.

Error:

The subreport 'attendance' could not be found at the specified location attendance. Please verify that the subreport has been published and that the name is correct.

My code:

public async Task<FileContentResult> Attendance(InputFilterDto input)
{
    var path = $"{this.host.WebRootPath}\\Reports\\attendanceForUser.rdlc";

    Stream reportDefinition = new FileStream(path, FileMode.Open, FileAccess.Read);

    LocalReport report = new LocalReport();
    var datasourses = await Datasourses(input);

    foreach (var item in datasourses.DataSources)
    {
        report.DataSources.Add(item);
    }

    report.LoadReportDefinition(reportDefinition);
    report.SubreportProcessing += (sender, e) =>
        {
            foreach (var item in datasourses.DataSources)
            {
                e.DataSources.Add(item);
            }
        };

    byte[] pdf = report.Render("PDF");
    var fileResult = new FileContentResult(pdf, "application/pdf")
        {
            FileDownloadName = DateTime.Now.ToString("yy-MM-dd-hh-mm")
        };

    return fileResult;
}

enter image description here

enter image description here

I asked gpt chat but it didn't help me to solve the problem


Solution

  • use this code:

    var path = $"{this.host.WebRootPath}\\Reports\\attendanceForUser.rdlc";
                var subReportpath = $"{this.host.WebRootPath}\\Reports\\attendance.rdlc";
                using (Stream reportDefinition = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    LocalReport report = new LocalReport();
                    var datasourses = await Datasourses(input);
                    report.LoadReportDefinition(reportDefinition);
                    using (Stream subReportDefinition = new FileStream(subReportpath, FileMode.Open, FileAccess.Read))
                    {
                        report.LoadSubreportDefinition("attendance", subReportDefinition);
                    }
                    foreach (var item in datasourses.DataSources)
                    {
                        report.DataSources.Add(item);
                    }
                    //report.SetParameters(parameters);
                    report.SubreportProcessing += (sender, e) =>
                    {
                        foreach (var item in datasourses.DataSources)
                        {
                            e.DataSources.Add(item);
                        }
                    };
                    byte[] pdf = report.Render("PDF");
                    var fileResult = new FileContentResult(pdf, "application/pdf")
                    {
                        FileDownloadName = DateTime.Now.ToString("yy-MM-dd-hh-mm")
                    };
                    return fileResult;
                }
    

    enter image description here