Microsoft's Entity Framework since its inception has become developers favorite for mapping a relational database to a conceptual model. Its strength lies in the way it allows developers to modify the conceptual model as per their business logic and still be connected to the relational database. The entity framework allows user to create relationships and hierarchies between entities and create additional mappings as required. ActiveReports, being the most powerful reporting engine provides capability to get bound to almost every data source. In this blog, we will discuss how to bind a parameterized PageReport to EntityFramework. Since the Entity Framework creates conceptual class objects from the database, we can use these class objects as datasource for our reports. Now, we will divide the implementation in three parts:
First step here is to create a parameterized page report. Please refer to the documentation for steps for creating a parametererized PageReport.
Now the final step for our implementation is to setup datasource for the report. Use the Entity list created in the last section as a datasource for the report. Here is the code for the same:
Sample\_Database1Entities \_db = new Sample_Database1Entities();
GrapeCity.ActiveReports.PageReport definition;
protected void Page_Load(object sender, EventArgs e)
{
string rptPath = Server.MapPath("PageReport2.rdlx");
definition = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(rptPath));
definition.Document.LocateDataSource += new LocateDataSourceEventHandler(runt_LocateDataSource);
WebViewer1.Report = definition;
}
void runt_LocateDataSource(object sender, GrapeCity.ActiveReports.LocateDataSourceEventArgs args)
{
int id = (int)args.Report.Parameters[0].CurrentValue;
var collect = _db.Students.Where(r => r.StudentID == id);
args.Data = collect.ToList();
}
After the above implementation, our report will look like this : For complete implementation, refer to the sample application. Download CSharp Sample Download VB Sample