简体   繁体   中英

Error trying to download Crystal Report PDF

I'm trying to implement Crystal Reports to my project

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:

rd.SetDataSource(allDatos);

With this error:

An exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll but was not handled in user code

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.

What am I doing wrong?

That is because you are writing this outside the using scope of your functional code block :

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.

Hope this helps.

omg, many thanks, you helped me alot!... answer: close using block at final of your code like this:

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();
    }
}
  }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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