简体   繁体   English

使用实体框架的MVC 4报告

[英]MVC 4 Reporting with Entity Framework

I'm using MVC4 and Entity Framework in order to develop an intranet web app and I have to create reports which can be dowloaded by the users. 我正在使用MVC4和Entity Framework来开发Intranet Web应用程序,并且我必须创建可由用户下载的报告。 In fact, I have 2 report templates (.rdlc files). 实际上,我有2个报告模板(.rdlc文件)。 One of them is a generic template which will be copied and modified for the next templates. 其中一个是通用模板,将为下一个模板进行复制和修改。 That's what I did with my "PersonReport" template : copied and modified from the generic. 那就是我对“ PersonReport”模板所做的:从通用文件复制和修改。

My problem is that with the genereic template (and the code below), everything's fine. 我的问题是,使用通用模板(以及下面的代码),一切都很好。 However, when I'm using my personalized template, it doest not work. 但是,当我使用个性化模板时,它不起作用。 I tried to debug my code and I found where is the issue (when I'm using the .Render() to specifiy the format). 我尝试调试代码,发现问题出在哪里(当我使用.Render()指定格式时)。 There is my error message : 有我的错误信息:

An error occurred during local report processing.

Here is my action : 这是我的动作:

public ActionResult PersonReport()
{

    ReportViewer personReportViewer = new ReportViewer();

    //Parameters

    ReportParameter[] reportParameters = new ReportParameter[3];
    reportParameters[0] = new ReportParameter("Title", "Test", true);
    reportParameters[1] = new ReportParameter("Address", "Avenue de Tervuren,  268", true);
    reportParameters[2] = new ReportParameter("PostalCode", "B-1150 Brussels", true);

    //Report Template

    personReportViewer.ProcessingMode = ProcessingMode.Local;
    personReportViewer.LocalReport.ReportPath = HttpContext.Server.MapPath("..") + "\\Reporting\\Templates\\" + "AllPersonsReport.rdlc";

    //Source

    DataSet ds = BuSIMaterial.Utils.Services.ExecuteStoredProcedure(db, "GetAllPersons", null);

    ReportDataSource dataSource = new ReportDataSource("personDataSource", ds.Tables[0]);
    personReportViewer.LocalReport.DataSources.Clear();
    personReportViewer.LocalReport.DataSources.Add(dataSource);

    //Report type

    personReportViewer.LocalReport.SetParameters(reportParameters);
    byte[] byteArray = personReportViewer.LocalReport.Render("PDF"); //Here's the failure

    //Downloading

    Response.ClearHeaders();
    Response.AddHeader("Content-Disposition", "attachment; Filename=\"" + DateTime.Now + "_Title_.pdf" + "\"");
    Response.AddHeader("Content-Transfer-Encoding", "Binary");
    Response.BinaryWrite((byte[])byteArray);
    Response.Flush();
    Response.End();

    return RedirectToAction("Index");
}

And my method to create a DataSet from a simple select * stored procedure : 我的方法是从一个简单的select *存储过程创建一个DataSet:

public static DataSet ExecuteStoredProcedure(ObjectContext db,
                                     string storedProcedureName,
                                     IEnumerable<SqlParameter> parameters)
{
    var connectionString =
        ((EntityConnection)db.Connection).StoreConnection.ConnectionString;

    var ds = new DataSet();

    using (var conn = new SqlConnection(connectionString))
    {
        using (var cmd = conn.CreateCommand())
        {
            cmd.CommandText = storedProcedureName;
            cmd.CommandType = CommandType.StoredProcedure;

            if (parameters != null)
            {
                foreach (var parameter in parameters)
                {
                    cmd.Parameters.Add(parameter);
                }
            }

            using (var adapter = new SqlDataAdapter(cmd))
            {
                adapter.Fill(ds);
            }
        }
    }

    return ds;
}

UPDATE : I wanted to check if my DataSet was filled and it isn't. 更新 :我想检查我的数据集是否已填充,但没有。 Could it be the problem? 可能是问题吗?

I think you should try adding more parameters to your Render() overload: 我认为您应该尝试向Render()重载添加更多参数:

    private Warning[] warnings;
    private string[] streams;
    private string mimeType;
    private string encoding;
    private string extension;

    byte[] byteArray = personReportViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streams, out warnings);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM