문제

I have a function that generates a report. It works. My problem is that I use MVC3 in C # and I can not insert a reportviewer in a file. Cshtml. Ascx I am using to try to show the report, but the following error occurs:

Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil ServerExecuteHttpHandlerWrapper +'

when I call o@Html.Partial ("relatorioApontamento") in relatorio.cshtml file.

relatorio.cshtml

@{
    ViewBag.Title = "relatorio";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script src="@Url.Content("~/Scripts/relatorio.js")" type="text/javascript"></script>
@using (Html.BeginForm("relatorio", "Paginas", FormMethod.Post, new { @id = "frmParametroConfigPath" }))
{
   <div id="relatorio">
        <h1 class="titulo">Relatório</h1>
        <div class="boxRecurso">
            <label id="lbl_recurso">Recurso:</label><br />
            <select ID="ddl_nm_recurso" class="txtRecurso"></select>
        </div>
        <div class="boxDataInicial">
            <label id="lbl_data_inicial">Data Inicial:</label><br />
            <input type="text" id="datepicker_ida" />
        </div>
        <div class="boxDataFinal">
            <label id="lbl_data_final">Data Final:</label><br />
            <input type="text" id="datepicker_volta" />
        </div>
        <div id="box_btnGerar">
            <input type="button" ID="btnGerar" class="botao" value="Gerar" />
        </div>
    </div>
    @Html.Partial("relatorioApontamento");

relatorioApontamento.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="relatorioApontamento.ascx.cs" Inherits="ControleBU.Views.Paginas.relatorioApontamento" %>
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
     <asp:ScriptManager ID="scriptManager" runat="server"></asp:ScriptManager>
<rsweb:ReportViewer ID="rv" runat="server" Height="679px" Width="1300px">
</rsweb:ReportViewer>

relatorioApontamento.ascx.cs

namespace ControleBU.Views.Paginas
{
    public partial class relatorioApontamento : System.Web.Mvc.ViewUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["item"] != null)
            {
                rv.LocalReport.ReportPath = "Reports\\Report1.rdlc";
                rv.LocalReport.DataSources.Add((ReportDataSource)Session["item"]);
                rv.LocalReport.SetParameters(new ReportParameter("TotalHoras", Convert.ToInt32(Math.Truncate(((TimeSpan)Session["TotalHoras"]).TotalHours)).ToString() + ":" + ((TimeSpan)Session["TotalHoras"]).Minutes.ToString()));
                rv.LocalReport.SetParameters(new ReportParameter("Ida", Convert.ToString(Session["DataInicio"])));
                rv.LocalReport.SetParameters(new ReportParameter("Volta", Convert.ToString(Session["DataFim"])));
                rv.LocalReport.Refresh();
            }

        }
    }
}

function in Paginas Controller

public int gerarRelatorioRelatorio(DateTime datepicker_ida, DateTime datepicker_volta, string ddl_nm_recurso)
        {
            try
            {
                ProjectBoxDAL dalProjectBox = new ProjectBoxDAL();
                Softbox.DashBordGBU.Reports.dtsReportRecurso dt = new Softbox.DashBordGBU.Reports.dtsReportRecurso();

                BUProjetosDAL dalBuProjetos = new BUProjetosDAL();

                int codRecurso = Convert.ToInt32(ddl_nm_recurso);
                int codCliente = dalBuProjetos.retornaCodigoClienteRecurso(codRecurso);

                IDataReader dr = dalProjectBox.relatorioRecurso(codCliente, datepicker_ida, datepicker_volta, codRecurso);

                dt.Tables[0].Load(dr);

                if (dt.Tables[0].Rows.Count > 0)
                {
                    var total = dt.ReportRecurso.AsEnumerable().Sum(x => x.horas_ms);

                    TimeSpan totalHoras = TimeSpan.FromMilliseconds(total);

                    Microsoft.Reporting.WebForms.ReportDataSource item = new Microsoft.Reporting.WebForms.ReportDataSource();
                    item.Value = dt.Tables[0];
                    item.Name = "ReportRecurso";

                    ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
                    Session["DataInicio"] = datepicker_ida;
                    Session["DataFim"] = datepicker_volta;
                    Session["ddl"] = ddl_nm_recurso;
                    Session["TotalHoras"] = totalHoras;
                    Session["Item"] = item;

                    return 1;
                }
                else
                    return 2;

            }
            catch (Exception)
            {
                return 0;
            }
        }
도움이 되었습니까?

해결책

You dont need new view or partial view to show your report :) just do the following : at the end of your Paginas controller add new method call print() and in print method define your report and print it as pdf like the following :

     public void Print()
                {
        LocalReport localReport = new LocalReport();
        localReport.ReportPath = @"report full path [Reports/myreport.rdlc]";
//if you have parameters set your parameters here
           Warning[] warnings;
                    string[] streamids;
                    string mimeType;
                    string encoding;
                    string filenameExtension;

                    byte[] rebytes = localReport.Render(
        "PDF", null, out mimeType, out encoding, out filenameExtension,
        out streamids, out warnings);

                    Response.Buffer = true;
                    Response.Clear();
                    Response.ContentType = mimeType;
                    Response.AddHeader("application/pdf", "attachment; filename= filename" + "." + filenameExtension);
                    Response.OutputStream.Write(rebytes, 0, rebytes.Length); // create the file  

                    Response.Flush(); // send it to the client to download  
                    Response.End();
        }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top