1
votes

I want to set a subreport at my main report but there is something wrong with my dataset of subreport. The action of my main report.

  public ActionResult ExibirRelatorioProgramaSol(FormCollection form)
    {
        DateTime dtInicial = DateTime.Parse(form["dt_inicio"]);
        DateTime dtFinal = DateTime.Parse(form["dt_fim"]);
        int idLista = form["ddl_Lista"].ConvertValueForm<int>();
        var avaliacao = _appServicoAvaliacaoSetor.ObterAvaliacoes(dtInicial, dtFinal, idLista);
        var relatorio = _appServicoAvaliacaoSetor.GerarRelatorioProgramaSol(dtInicial, dtFinal, idLista);

        var viewer = new Microsoft.Reporting.WebForms.ReportViewer();
        viewer.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local;
        viewer.LocalReport.ReportPath = Request.MapPath(Request.ApplicationPath) + "/Report/ReportProgramaSol.rdlc";
        viewer.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(RenderizaSubRelatorioPedido);
        viewer.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("ProgramaSol", relatorio.ToList()));
        viewer.SizeToReportContent = true;
        viewer.Width = System.Web.UI.WebControls.Unit.Percentage(100);
        viewer.Height = System.Web.UI.WebControls.Unit.Percentage(100);

        ViewBag.ReportViewer = viewer;

        return View();
    }

The action of my subreport:

private void RenderizaSubRelatorioPedido(object sender, SubreportProcessingEventArgs e)
    {
        int idEmpresa = Convert.ToInt32("2");

        e.DataSources.Add(new ReportDataSource("empresa",
                      _appServicoEmpresa.ObterEntidadePor(i => i.Id == idEmpresa)));
    }

The error:

Failure data recovery for the sub- report ' subreport1 ' , located at: C : \ Projects \ Samich \ VisualStudioOnLine \ Vanessa \ Samich Projects C # \ EMS - Audit Management System - Copy \ EMS - Audit Management System \ Report \ ReportHeader.rdlc . Check the log files for more information .

Can someone help me?

2
Ah subreports...they yield so little in terms of useful error messages. One thing I've done is to start with a totally clean sub-report (e.g. no datasources/parameters or whatever) and then slowly...VERY SLOWLY...add the different pieces. Start with parameters...you'll need to bind those in the designer and they'll likely come from the outer report. Then add a datasource. See what dies and go from there.BlackjacketMack
I tried everything but nothing works. I really need help. =(Vanessa Mello Souza

2 Answers

2
votes

Solution Found

An ASPX form "ReportViewerWebForm.aspx" is added to the project by NuGet. This is called from the MVC view to render the report. Editing this page allows the sub-report process to be called.

ReportViewerWebForm.aspx does not have a code-behind page but inherits from a class "ReportViewerForMvc.ReportViewerWebForm" that operates behind the scenes.

I added a page class file "ReportViewerWebForm.aspx.cs" (and designer file "ReportViewerWebForm.aspx.designer.cs" for completeness) and changed the ASPX page to inherit from the code-behind class.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ReportViewerWebForm.aspx.cs" Inherits="YourNamespace.ReportViewerWebForm" %>

I updated the code-behind so that the page class inherits from "ReportViewerForMvc.ReportViewerWebForm" and created an override for On_Load to wire up the sub-report processing event. Within this event handler is where data is added to the sub-report

using Microsoft.Reporting.WebForms;
namespace YourNamespace
{
    public partial class ReportViewerWebForm : ReportViewerForMvc.ReportViewerWebForm
    {

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            ReportViewer1.LocalReport.SubreportProcessing += LocalReport_SubreportProcessing;
        }


        private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
        {
            ReportDataSource rds = new ReportDataSource("SubReport_DatasetName_Here", yourData);
            e.DataSources.Add(rds);
        }
    }
}

This works fine if you have just one report, but in order to support multiple reports, you'll need to know which data to supply in each case. In the short term I'll set a unique name in "Reportviewer1.LocalReport.DisplayName" as I have only a few reports to support.

0
votes

I have the same issue as above - here is what I've tried so far

An existing Windows application (which I am migrating to MVC) has RDLC reports which render OK in the Windows Report Viewer control.

Porting the code to MVC has issues with the sub-report as above. Can't find any logs or other pointers to the issue, just the message about unable to render the sub-report.

I'm using the following viewer

@using ReportViewerForMvc;

The sub-report data is added in the processing event

rpt.SubreportProcessing += LocalReport_SubreportProcessing;

In the MVC version, the above event is never called in the Controller.

In another attempt to resolve this I moved the C# to a code block on the CSHTML page and wrapped the event and other methods in delegates and Action blocks, but get the same issue.

rpt.SubreportProcessing += (object sender, SubreportProcessingEventArgs e) =>
    {
...

I don't know where the report process runs but none of the controls event handlers are ever called in either of the Controller or CSHTML code.

If render out a report as a PDF in the Controller instead of returning a View, the report is correctly created and a PDF is displayed in the browser.

    Warning[] warnings;
    string mimeType;
    string[] streamids;
    string encoding;
    string filenameExtension;         

    var bytes = rpt.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);

    return new System.Web.Mvc.FileContentResult(bytes, mimeType);

I've also created a separate APSX page running the same code as the Windows version which calls the sub-report event handlers and appears to have all of the required data processed. However, No report is displayed in the report viewer (despite it saying there is one page). In the ASPX page designer, the control has an error message saying it can't be created (and there isn't a report viewer control in the toolbox)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Reports.aspx.cs" Inherits="Cass3Web.Reports" %>

<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Scripts>
                <asp:ScriptReference Assembly="ReportViewerForMvc" Name="ReportViewerForMvc.Scripts.PostMessage.js" />
            </Scripts>                
        </asp:ScriptManager>
       <div style="height: 600px; width: 900px; border: 1px solid silver;">
            <rsweb:ReportViewer ID="rv" runat="server" Height="600" Width="900" AsyncRendering="false"></rsweb:ReportViewer>
        </div>
    </form>
</body>
</html>

So far then, no luck

I notice that when the MVC page renders, the report does not immediately load. It appears to wait on a timer and after some seconds, shows the busy icon and continues to process the report (minus the sub-reports)

Will give this another look but may have to go another way if I can't get success