ActiveReports 18 .NET Edition
Developers / Work with Reports using Code / Page/RDLX Report / Bind a Page/RDLX Report to Data / Bind Page/RDLX Report to Entity Framework and Other Data Providers
In This Topic
    Bind Page/RDLX Report to Entity Framework and Other Data Providers
    In This Topic

    You can bind your Page/RDLX reports to a data provider like Entity Framework, NHibernate, etc.

    To connect to the Entity Framework data source in ActiveRepors, follow these basic steps:

    1. Initialize a selected data provider.
    2. Сreate a data layer that uses the selected data provider.
    3. Bind the data that you want to link to the report, using the data binding mechanism with objects. For more information, see the Object Provider section below.

    Below is the example that illustrates binding to the Entity Framework in details. The example assumes that you have a database with the Product entity, which has ProductName and ProductID fields. A tuple of data retrieved from the database needs to be mapped to a table in a report.

    Note: The sample report has already pre-configured fields in the table to match the fields from the entities.
    1. Initialize Entity Framework as a data provider.
      Copy Code
      // Your binding model
      private class Product
      {
          // Key
          public int ProductID { get; set; }
          // Fields
          public string ProductName { get; set; }
      }
             
      // Your database context
      private class ProductsDatabaseContext : DbContext
      {
          public ProductsDatabaseContext(string connectionstring) : base(connectionstring) { }
          public DbSet<Product> Products { get; set; }
      }
      
    2. Create a data layer.
      Copy Code
      // Your data layer
      private class ProductDataLayer
      {
          private ProductsDatabaseContext _context;
          internal ProductDataLayer()
          {
              _context = CreateDataProvider();
          }
          private ProductsDatabaseContext CreateDataProvider()
          {
              // Your connection string to database
              string connectionString = "";
              return new ProductsDatabaseContext(connectionString);
          }
          internal IEnumerable<Product> GetProducts() => _context?.Products;
      }
      
    3. Bind data by the data binding mechanism that uses objects.
      Copy Code
      private PageReport LoadReport()
      {
          // Your path to the report
          string path = "";
         
          PageReport report = new PageReport(new FileInfo(path));
          report.Document.LocateDataSource += OnLocateDataSource;
         
          return report;
      }
      private void OnLocateDataSource(object sender, LocateDataSourceEventArgs args)
      {
          ProductDataLayer dataLayer = new ProductDataLayer();
          args.Data = dataLayer.GetProducts();
      }
      

    This approach allows you to work with any data provider and gives you the flexibility to customize your applications when working with reports. 

    DataSet Provider

    To bind your Page/RDLX report to a DataSet, you must subscribe to the event that occurs when a report needs to locate a data source from the calling application:

    Copy Code
    report.Document.LocateDataSource += new LocateDataSourceEventHandler(OnLocateDataSource);
    

    To connect to an unbound data source at run time, you can use the DataSet provider with the LocateDataSource event. The reporting engine raises the LocateDataSource event when it needs input on the data to use.

    Copy Code
    private PageReport LoadReport()
    {
        string path = ""; // Your path to the report
        PageReport report = new PageReport(new FileInfo(path));
        report.Document.LocateDataSource += new LocateDataSourceEventHandler(OnLocateDataSource);
       
        return report;
    }
    private void OnLocateDataSource(object sender, LocateDataSourceEventArgs args)
    {
        DataTable dataTable = new DataTable("Example");
       
        // Add the desired data to the dataset
        //
        // dataTable.Columns.Add(...);
        // dataTable.Rows.Add(...);
        //
       
        args.Data = dataTable;
    }
    
    Note: We assume that the report has defined data fields and datasets, which can be done by various ways, e.g., in the Web or Windows Forms Designer.

    The DataSet provider has the following limitations:

    To request a field from a parent table, prefix the field name with the name of the relation(s) that must be traversed to navigate to the appropriate parent table. Separate field names and relations with periods.

    For example, consider a main table named OrderDetails with a parent table named Orders. A relation named Orders_OrderDetails defines the relationship between the two tables. Use a field with the syntax below to access the OrderDate from the parent table: Orders_OrderDetails.OrderDate.

    Use this same technique to traverse multiple levels of table relations. For example, consider that the Orders table, used in the example above, has a parent table named Customers and a relation, binding the two, called Customers_Orders. If the CommandText specifies the main table as OrderDetails, use the following syntax to get the CustomerName field from the parent table: Customers_Orders.Orders_OrderDetails.CustomerName.

    Note: An ambiguity can occur if a field and a relation have the same name. This is not supported.

    Object Provider

    Use the API to bind a Page/RDLX report to the Object data source.

    In the example below, the report calls the LocateDataSource event, used to set data into the report. To do this, we create the OnLocateDataSource event handler and pass in it the data that will be transmitted to the report. The data is added as a new data source in the LocateDataSource event handler.

    Copy Code
    private PageReport LoadReport()
    {
        string path = ""; // Your path to the report
        PageReport report = new PageReport(new FileInfo(path));
        report.Document.LocateDataSource += new LocateDataSourceEventHandler(OnLocateDataSource);
        return report;
    }
    private void OnLocateDataSource(object sender, LocateDataSourceEventArgs args)
    {
        ArrayList data = new ArrayList();
       
        // Add the desired data to the collection
        //
        // data.Add(...);
        //
       
        args.Data = data;
    }
    

    Note: We assume that the report has defined data fields and datasets, which can be done by various ways, e.g., in the Web or Windows Forms Designer.