0

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

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

  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jun 04 '23 at 20:40