An error occurred during report processing. -RLDC reporting in ASP.NET MVC

旧时模样 提交于 2019-12-17 05:13:39

问题


I have this action to generate reports :

  public ActionResult Report(string id)
        {
            LocalReport lr = new LocalReport();
            string path = Path.Combine(Server.MapPath("~/Report"), "Person.rdlc");
            if (System.IO.File.Exists(path))
            {
                lr.ReportPath = path;
            }
            else
            {
                return View("Index");
            }
            List<Person> cm = new List<Person>();

            var viewModel = new PersonIndexData();

            viewModel.People= db.Person
            .Include(k => k.Groups)
            .OrderBy(k => k.Name);

            cm = viewModel.People.ToList();

            ReportDataSource rd = new ReportDataSource("PersonDataSet", cm);
            lr.DataSources.Add(rd);
            string reportType = id;
            string mimeType;
            string encoding;
            string fileNameExtension;

            Warning[] warnings;
            string[] streams;
            byte[] renderedBytes;

            renderedBytes = lr.Render(
                reportType,
                null,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);


            return File(renderedBytes, mimeType);
        }

When I call this action like this : (mysite/person/report/pdf), I get this exception :

An error occurred during report processing. Indicating this line :

        renderedBytes = lr.Render(
            reportType,
            deviceInfo,
            out mimeType,
            out encoding,
            out fileNameExtension,
            out streams,
            out warnings);

Can you tell me why I'm getting this exception in this code? It doesn't give any error and the exception is not very explanatory. I'm using EF-code first. Thank you.


回答1:


Have you try like this after adding a TableAdapter? It is working perfectly for me.

public FileResult Report(string id)
{
    PersonTableAdapter ta = new PersonTableAdapter();
    PersonDataSet ds = new PersonDataSet();

    //for avoiding "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints." error
    ds.Person.Clear();
    ds.EnforceConstraints = false;

    ta.Fill(ds.Person, id); //You might customize your data at this step i.e. applying a filter

    ReportDataSource rds = new ReportDataSource();
    rds.Name = "ReportingDataSet";
    rds.Value = ds.Person;

    ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
    rv.ProcessingMode = ProcessingMode.Local;
    rv.LocalReport.ReportPath = Server.MapPath("~/Report/Person.rdlc");

    // Add the new report datasource to the report.
    rv.LocalReport.DataSources.Add(rds);
    rv.LocalReport.EnableHyperlinks = true;
    rv.LocalReport.Refresh();

    byte[] streamBytes = null;
    string mimeType = "";
    string encoding = "";
    string filenameExtension = "";
    string[] streamids = null;
    Warning[] warnings = null;

    streamBytes = rv.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);

    return File(streamBytes, mimeType, "Person" + "_" + id + ".pdf");
}

Hope this helps...




回答2:


I'm not sure which one will help you, so I list them as following:

  1. Try removing the datasource from the Report menu and then adding it again
  2. Check Render function requirements before action, so you may wanna check in and out parameter values.
  3. Try to see some examples around LocalReporter to be more aware how to use this tool.

And Finally, The line which throws exception is not the same as main code, since you place null instead of deviceInfo! regards.




回答3:


My solution for rdlc is based on ReportViewer.

        byte[] bytes = null;
        string attachmentName = string.Empty;

        Warning[] warnings;
        string[] streamids;
        string mimeType;
        string encoding;
        string extension;
        /*GetReportDataSources(logicResult, spec);*/
        var reportPath = GetReportExecutionPath();

        ReportViewer viewer = new ReportViewer();
        /*viewer.LocalReport.SubreportProcessing +=
            new Microsoft.Reporting.WinForms.SubreportProcessingEventHandler(LocalReport_SubreportProcessing);*/
        viewer.LocalReport.ReportPath = reportPath;
        /*viewer.LocalReport.SetParameters(parameters);*/
        viewer.LocalReport.EnableExternalImages = true;
        viewer.RefreshReport();

        viewer.LocalReport.DisplayName = "displayName";
        bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension,
            out streamids, out warnings);

/*some logic */

        return new StreamResponse(() =>
        {
            var pdfOutput = new MemoryStream(bytes); 
            pdfOutput.Seek(0, SeekOrigin.Begin);
            return pdfOutput;
        }, "application/pdf").WithHeader("Content-Disposition", "attachment; filename=" + attachmentName);

You need also add report path, and datasources (my solution is complicated, and I use for that LocalReport_SubreportProcessing).

P.S. Using rdlc with ASP.MVC have 1 issue. You need set on ISS App Pool "Identity = LocalSystem", that is ok in Intranet, but not ok in Internet.



来源:https://stackoverflow.com/questions/28966954/an-error-occurred-during-report-processing-rldc-reporting-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!