In my application, I have created a report in HTML view.
For that, I have used the view model to load the data.
Now to convert it to the pdf and save it to the file I wanted to create a crystal report using the same view model.
So I created a new crystal report.
Then I choose my view data models from the .net project list in the crystal report wizard.
For this, there are two view models I'm using. and then I created the report.
This is the code I run to show the HTML view report.
public ActionResult Index(int id)
{
AppRequest appRequest = db.AppRequest.Find(id);
if (appRequest.General.Count != 0)
{
List<ReportViewModel>RequestView = new List<ReportViewModel>();
var ReqDetails = (from a in db.AppRequest
join e in db.CreateEmployee on a.Req_By equals e.Id
join c in db.CreateCompany on e.CompanyId equals c.Id
join d in db.CreateDesignation on e.DesignId equals d.Id
join dep in db.CreateDepartment on e.DepId equals dep.Id
join g in db.General on a.Id equals g.Req_Id
where a.Id == id
select new ReportViewModel
{
RequestedDate = a.Req_Date.ToString(),
RequestBy = e.EmpName,
CompanyName = c.CompanyName,
RequestByDessignation = d.Designation,
RequestByDepartment = dep.Department,
ApprovalId = g.ApprovedNumber,
CoverNote = a.Req_CoverNote,
GeneralItemsApprovedList = (from a in db.AppRequest
join g in db.General on a.Id equals g.Req_Id
join gi in db.GeneralItms on g.Id equals gi.General_Id
where a.Id == id && gi.IsApproved == true
select new GeneralItemsApprovedList
{
ItemDescription = gi.Attachment_Description,
ItemPrice = gi.Attachment_Amount.ToString(),
GenId = gi.Id
}).ToList(),
ApprovalList = (from a in db.AppRequest
join ap in db.ApprovalProcess on a.Id equals ap.Req_Id
join pa in db.ApproveParties on ap.Id equals pa.ApprovalProcess_Id
join ae in db.CreateEmployee on pa.Approver_Id equals ae.Id
join ad in db.CreateDesignation on ae.DesignId equals ad.Id
where a.Id == id
select new ApprovalList
{
ApproveDate = pa.Approved_Date.ToString(),
ApprovedBy = ae.EmpName,
ApprovedByDessignation = ad.Designation,
}).ToList()
}).ToList();
return View("ApprovedReport", ReqDetails);
}
So same ReqDetails I pass it to the crystal report data source.
CrystalReports.cryGeneralRequest rpt = new CrystalReports.cryGeneralRequest();
rpt.Load(Path.Combine(Server.MapPath("~/CrystalReports"), "cryGeneralRequest.rpt"));
rpt.SetDataSource(ReqDetails);
Response.Buffer = false;
Response.ClearContent();
Response.ClearHeaders();
try
{
Stream steam = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
steam.Seek(0, SeekOrigin.Begin);
return File(steam, "application/pdf", "General.pdf");
}
catch (Exception ex)
{
throw;
}
in the Streamline it returns an error "Database logon failed".
This is the first time I'm trying to create a crystal report in asp MVC project. I want to know Is possible to pass data from the view model to the crystal report as I did here? and any solutions will be grateful. Thanks.