简体   繁体   English

尝试下载Crystal Report PDF时出错

[英]Error trying to download Crystal Report PDF

I'm trying to implement Crystal Reports to my project 我正在尝试为我的项目实施Crystal Reports

My Home Controller: 我的家庭控制器:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Biblioteca.Models;
using CrystalDecisions.CrystalReports.Engine;

namespace Biblioteca.Controllers
{
    public class HomeController : Controller
    {
        private BibliotecaDatabase db = new BibliotecaDatabase();

        public ActionResult Index()
        {
            var totalesviewmodel = new TotalesViewModel();
            totalesviewmodel.MontoCopias = db.AlumnosList.Sum(o => o.Copias);
            totalesviewmodel.MontoImpresiones = db.AlumnosList.Sum(o => o.Impresiones);
            totalesviewmodel.DineroDeposito = db.AlumnosList.Sum(o => o.Deposito);
            totalesviewmodel.DineroSap = db.AlumnosList.Sum(o => o.Sap);
            totalesviewmodel.MontoCopiasMaestro = db.MaestrosList.Sum(o => o.Copias);
            totalesviewmodel.MontoImpresionesMaestro = db.MaestrosList.Sum(o => o.Impresiones);
            return View(totalesviewmodel);
        }

        public ActionResult Reports()
        {
            List<Alumno> allDatos = new List<Alumno>();
            using (BibliotecaEntities dc = new BibliotecaEntities())
            {
                allDatos = dc.Alumnos.ToList();
            }
            return View(allDatos);
        }

        public ActionResult ExportReport()
        {
            List<Alumno> allDatos = new List<Alumno>();
            using (BibliotecaEntities dc = new BibliotecaEntities())
            {
                allDatos = dc.Alumnos.ToList();
            }

            ReportDocument rd = new ReportDocument();
            rd.Load(Path.Combine(Server.MapPath("~/Reports"), "rpt_datos.rpt"));
            rd.SetDataSource(allDatos);

            Response.Buffer = false;
            Response.ClearContent();
            Response.ClearHeaders();

            try
            {
                Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
                stream.Seek(0, SeekOrigin.Begin);
                return File(stream, "application/pdf", "DatoList.pdf");
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public ActionResult Contact ()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

When I try to download a PDF file of my report, my project stops at: 当我尝试下载报告的PDF文件时,我的项目在以下位置停止:

rd.SetDataSource(allDatos);

With this error: 出现此错误:

An exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll but was not handled in user code mscorlib.dll中发生类型'System.Reflection.TargetInvocationException'的异常,但未在用户代码中处理

When I click continue my browser says: 当我单击继续时,浏览器会显示:

The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. ObjectContext实例已被处置,不能再用于需要连接的操作。

What am I doing wrong? 我究竟做错了什么?

That is because you are writing this outside the using scope of your functional code block : 这是因为您是在功能代码块的using范围之外编写此代码的:

rd.SetDataSource(allDatos);

Try writing those code lines into the using code block or you can remove using if that is not an issue in your case. 尝试将这些代码行写入using代码块中,或者如果这不是问题,则可以删除using

Hope this helps. 希望这可以帮助。

omg, many thanks, you helped me alot!... answer: close using block at final of your code like this: omg,非常感谢,您为我提供了很多帮助!...答案:像这样在代码的最后关闭使用block:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Biblioteca.Models;
using CrystalDecisions.CrystalReports.Engine;

namespace Biblioteca.Controllers
{
    public class HomeController : Controller
{
    private BibliotecaDatabase db = new BibliotecaDatabase();

    public ActionResult Index()
    {
        var totalesviewmodel = new TotalesViewModel();
        totalesviewmodel.MontoCopias = db.AlumnosList.Sum(o => o.Copias);
        totalesviewmodel.MontoImpresiones = db.AlumnosList.Sum(o => o.Impresiones);
        totalesviewmodel.DineroDeposito = db.AlumnosList.Sum(o => o.Deposito);
        totalesviewmodel.DineroSap = db.AlumnosList.Sum(o => o.Sap);
        totalesviewmodel.MontoCopiasMaestro = db.MaestrosList.Sum(o => o.Copias);
        totalesviewmodel.MontoImpresionesMaestro = db.MaestrosList.Sum(o => o.Impresiones);
        return View(totalesviewmodel);
    }

    public ActionResult Reports()
    {
        List<Alumno> allDatos = new List<Alumno>();
        using (BibliotecaEntities dc = new BibliotecaEntities())
        {
            allDatos = dc.Alumnos.ToList();
        }
        return View(allDatos);
    }

    public ActionResult ExportReport()
    {
        List<Alumno> allDatos = new List<Alumno>();
        using (BibliotecaEntities dc = new BibliotecaEntities())
        {
            allDatos = dc.Alumnos.ToList();


        ReportDocument rd = new ReportDocument();
        rd.Load(Path.Combine(Server.MapPath("~/Reports"), "rpt_datos.rpt"));
        rd.SetDataSource(allDatos);

        Response.Buffer = false;
        Response.ClearContent();
        Response.ClearHeaders();

        try
        {
            Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
            stream.Seek(0, SeekOrigin.Begin);
            return File(stream, "application/pdf", "DatoList.pdf");
        }
        catch (Exception ex)
        {
            throw;
        }
    }

    public ActionResult Contact ()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}
  }
  }

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

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