我有这个动作来生成报告:
public ActionResult Report(string id)
{
LocalReport lr = new LocalReport();
string path = Path.Combine(Server.MapPath("~/Report"), "Person.rdlc");
if (System.IO.File.Exists(path))
{
lr.ReportPath = path;
}
else
{
return View("Index");
}
List<Person> cm = new List<Person>();
var viewModel = new PersonIndexData();
viewModel.People= db.Person
.Include(k => k.Groups)
.OrderBy(k => k.Name);
cm = viewModel.People.ToList();
ReportDataSource rd = new ReportDataSource("PersonDataSet", cm);
lr.DataSources.Add(rd);
string reportType = id;
string mimeType;
string encoding;
string fileNameExtension;
Warning[] warnings;
string[] streams;
byte[] renderedBytes;
renderedBytes = lr.Render(
reportType,
null,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
return File(renderedBytes, mimeType);
}
当我这样调用此动作:(mysite / person / report / pdf)时,出现此异常:
报表处理期间发生错误。指示这一行:
renderedBytes = lr.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
你能告诉我为什么我在这段代码中得到这个异常吗?它没有给出任何错误,并且异常不是很解释。我先使用EF代码。谢谢你。
添加TableAdapter之后,您是否尝试过这种方法?它对我来说非常好。
public FileResult Report(string id)
{
PersonTableAdapter ta = new PersonTableAdapter();
PersonDataSet ds = new PersonDataSet();
//for avoiding "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints." error
ds.Person.Clear();
ds.EnforceConstraints = false;
ta.Fill(ds.Person, id); //You might customize your data at this step i.e. applying a filter
ReportDataSource rds = new ReportDataSource();
rds.Name = "ReportingDataSet";
rds.Value = ds.Person;
ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
rv.ProcessingMode = ProcessingMode.Local;
rv.LocalReport.ReportPath = Server.MapPath("~/Report/Person.rdlc");
// Add the new report datasource to the report.
rv.LocalReport.DataSources.Add(rds);
rv.LocalReport.EnableHyperlinks = true;
rv.LocalReport.Refresh();
byte[] streamBytes = null;
string mimeType = "";
string encoding = "";
string filenameExtension = "";
string[] streamids = null;
Warning[] warnings = null;
streamBytes = rv.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
return File(streamBytes, mimeType, "Person" + "_" + id + ".pdf");
}
希望这可以帮助...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句