我正在尝试将Crystal Reports实现到我的项目
我的家庭控制器:
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();
}
}
}
当我尝试下载报告的PDF文件时,我的项目停在:
rd.SetDataSource(allDatos);
出现此错误:
mscorlib.dll中发生了'System.Reflection.TargetInvocationException'类型的异常,但未在用户代码中处理
当我点击继续时,我的浏览器会说:
ObjectContext实例已被释放,不能再用于需要连接的操作。
我做错了什么?
答案 0 :(得分:1)
这是因为你在函数代码块的using
范围之外写了这个:
rd.SetDataSource(allDatos);
尝试将这些代码行写入using
代码块,或者如果您的情况不是问题,则可以删除using
。
希望这有帮助。
答案 1 :(得分:0)
omg,非常感谢,你帮助了我很多!...回答:在你的代码最后使用块关闭这样:
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();
}
}
}
}