Skip to main content Skip to footer

Using Parameterized Page Report with EntityFramework

BackGround

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:

  1. Create a parameterized Page Report
  2. Create Entity Model
  3. Setup this Entity as data source of the report

Create Parameterized Page Report

First step here is to create a parameterized page report. Please refer to the documentation for steps for creating a parametererized PageReport.

Create Entity Model

  1. Create classes for the database model using the Entity Framework. Refer to this link for how to create a database model.
  2. Instantiate the database model class which was created in Step 1.
  3. Create a strongly typed list of the Entity that is part of the model class.

Set DataSource for Page Report

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 : Results For complete implementation, refer to the sample application. Download CSharp Sample Download VB Sample

MESCIUS inc.

comments powered by Disqus