Reputation: 455
I have process that should show a report using Crystal Reports and in my project i used .NET MVC as my framework and Entity Framework for database models.
The problem is i do not know how to send dataset into my report, as i know that crystal report supported datatable as their dataset, so i need to convert linq result into datatable, here is my code so far:
IEnumerable<DataRow> SalesContractEntity = _db.Generate_Report(202, "RGBCtestcontract2");
ReportDocument rd = new ReportDocument();
rd.Load(Server.MapPath("~/Reports/") + "ColdLineBaliHolidayClubENUSD.rpt");
rd.SetDataSource(SalesContractEntity);
CrystalReportViewerID.ReportSource = rd;
Note:
System.Data.Objects.ObjectResult to System.Collections.Generic.IEnumerable
Upvotes: 0
Views: 621
Reputation: 455
finally i found the solution
here is my code:
List<Generate_Report_Result> ReportList = new List<Generate_Report_Result>();
ReportList = _db.Generate_Report(ApplicantID, ContractNumber).ToList();
DataTable datanya = ToDataTable(ReportList);
ReportDocument rd = new ReportDocument();
string ThePath = Server.MapPath("~/Reports/") + ReportFileName;
rd.Load(ThePath);
rd.SetDataSource(datanya);
CrystalReportViewerID.ReportSource = rd;
CrystalReportViewerID.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
CrystalReportViewerID.RefreshReport();
private DataTable ToDataTable(List<Generate_Report_Result> items)
{
var tb = new DataTable(typeof(Generate_Report_Result).Name);
PropertyInfo[] props = typeof(Generate_Report_Result).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var prop in props)
{
tb.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
}
foreach (var item in items)
{
var values = new object[props.Length];
for (var i = 0; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
}
tb.Rows.Add(values);
}
return tb;
}
and dont forget to add aspnet_client on our project folder (you can copy from C:\inetpub\wwwroot)
Upvotes: 1