Hi All,
I am creating a ASP.NET MVC 2 application where I want to generate a PDF report I have this code in my Report controller:
using System;
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Ajax; using System.Text; using System.Linq.Expressions; using Microsoft.Reporting.WebForms; using BournemouthUniversity.Domain.Abstract; using BournemouthUniversity.Domain.Entities;
namespace BournemouthUniversity.WebUI.Controllers { public class ReportsController : Controller { private ISqlStudentRepository sqlStudentRepository;
public ReportsController(ISqlStudentRepository sqlStudentRepository)
{
this.sqlStudentRepository = sqlStudentRepository;
}
//
// GET: /Reports/
public ActionResult Index()
{
return View("Index");
}
public ActionResult ALNReport()
{
var ReportPath = Server.MapPath("~/Views/Reports/StudentReport.rdlc");
object studentProblems = sqlStudentRepository.Students.Where(s => s.AssistedLearningNeeds == true).ToList();
string dataSet = "StudentProblems";
string filename = "ALN Problem Report " + DateTime.Now.ToShortDateString();
RenderReport(ReportPath, studentProblems, dataSet, filename);
return View();
}
public ActionResult StudentReport(int studentId)
{
var ReportPath = Server.MapPath("~/Views/Reports/StudentReport.rdlc");
object student = sqlStudentRepository.GetStudent(studentId);
string dataSet = "StudentProblems";
string filename = "Student Problems " + DateTime.Now.ToShortDateString();
RenderReport(ReportPath, student, dataSet, filename);
return View();
}
public ActionResult AllStudentsReport()
{
var ReportPath = Server.MapPath("~/Views/Reports/StudentReport.rdlc");
object studentProblems = sqlStudentRepository.Students.ToList();
string dataSet = "StudentProblems";
string filename = "All Student Problems Report " + DateTime.Now.ToShortDateString();
RenderReport(ReportPath, studentProblems, dataSet, filename);
return View();
}
private void RenderReport(string ReportPath, object Model, string dataSet, string filename)
{
var localReport = new LocalReport { ReportPath = ReportPath };
//Give the collection a name (EmployeeCollection) so that we can reference it in our report designer
var reportDataSource = new ReportDataSource(dataSet, Model);
localReport.DataSources.Add(reportDataSource);
var reportType = "PDF";
string mimeType;
string encoding;
string fileNameExtension;
//The DeviceInfo settings should be changed based on the reportType
//http://msdn2.microsoft.com/en-us/library/ms155397.aspx
var deviceInfo =
string.Format("<DeviceInfo><OutputFormat>{0}</OutputFormat><PageWidth>8.3in</PageWidth><PageHeight>11.0in</PageHeight><MarginTop>0.5in</MarginTop><MarginLeft>1in</MarginLeft><MarginRight>1in</MarginRight><MarginBottom>0.5in</MarginBottom></DeviceInfo>", reportType);
Warning[] warnings;
string[] streams;
byte[] renderedBytes;
//Render the report
renderedBytes = localReport.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
//Clear the response stream and write the bytes to the outputstream
//Set content-disposition to "attachment" so that user is prompted to take an action
//on the file (open or save)
String date = DateTime.Now.ToShortDateString();
String fileDate = date.Replace('/', '-');
filename = filename + fileDate;
filename = filename.Replace(' ', '_');
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=" + filename + "." + fileNameExtension);
Response.BinaryWrite(renderedBytes);
Response.End();
}
}
}
I have used code similar to this to produce PDF reports before but using a View in a database not an object. I am not able to select a student object as my datasource in the RDLC template. Is their any solutions for this?
Thanks,
Jonathan