Developers / Work with Reports using Code / Section Report / Bind a Section Report to Data / Bind a Section Report to Data Source at Run Time
Bind a Section Report to Data Source at Run Time

You can bind a section report to data by changing the data source at run time.

Create and Configure OLEDB Data Source

  1. Double-click in the gray area below rptModifyDS to create an event-handling method for the ReportStart event.
  2. Add code to the handler to change the data source at run time.
    Visual Basic.NET code. Paste JUST ABOVE the ReportStart event
    Copy Code
    Dim conn As System.Data.OleDb.OleDbConnection
    Dim reader As System.Data.OleDb.OleDbDataReader                                        
    
    Visual Basic.NET code. Paste INSIDE the ReportStart event
    Copy Code
    Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "[User Folder]\Samples19\Data\NWIND.mdb"
    conn = New System.Data.OleDb.OleDbConnection(connString)
    Dim cmd As New System.Data.OleDb.OleDbCommand("SELECT * FROM Products WHERE UnitPrice = 18", conn)
    conn.Open()
    reader = cmd.ExecuteReader()
    Me.DataSource = reader
    
    C# code. Paste JUST ABOVE the ReportStart event
    Copy Code
    private static System.Data.OleDb.OleDbConnection conn;
    private static System.Data.OleDb.OleDbDataReader reader;
    
    C# code. Paste INSIDE the ReportStart event
    Copy Code
    string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @"[User Folder]\Samples19\Data\NWIND.mdb";
    conn = new System.Data.OleDb.OleDbConnection(connString);
    System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("SELECT * FROM Products WHERE UnitPrice = 18", conn);
    conn.Open();
    reader = cmd.ExecuteReader();
    this.DataSource = reader;
    

Close the data connection (Visual Basic)

  1. In design view of rptModifyDS, drop down the field at the top left of the code view and select (rptModifyDS Events).
  2. Drop down the field at the top right of the code view and select ReportEnd. This creates an event-handling method for ReportEnd event.
  3. Add code to the handler to close the data connection.
    Visual Basic.NET code. Paste INSIDE the ReportEnd event
    Copy Code
    reader.Close()
    conn.Close()
    

Close the data connection (CS)

  1. Click in the gray area below rptModifyDS to select the report.
  2. Click the events icon in the Properties Panel to display available events for the report.
  3. Double-click ReportEnd. This creates an event-handling method for the ReportEnd event.
  4. Add code to the handler to close the data connection.
    C# code. Paste INSIDE the ReportEnd event
    Copy Code
    reader.Close();
    conn.Close();
    

See the UnboundData sample for details on how to use the FetchData event to display the report unbound data.

Create and Configure JSON Data Source

You can bind a section report to a JSON data source at run time using code.    

  1. Double-click in the gray area below SectionReport1 to create an event-handling method for the ReportStart event.
  2. Add code to the handler to change the data source at run time.
    Visual Basic.NET code. Paste JUST ABOVE the ReportStart event
    Copy Code
    Dim jsonDS As GrapeCity.ActiveReports.Data.JsonDataSource = New GrapeCity.ActiveReports.Data.JsonDataSource()
    jsonDS.ConnectionString = "jsondoc=https://demodata.mescius.io/northwind/odata/v1/Employees"
    jsonDS.JsonPath = "$.value[*]"
    Me.DataSource = jsonDS
    
    C# code
    Copy Code
    GrapeCity.ActiveReports.Data.JsonDataSource jsonDS = new GrapeCity.ActiveReports.Data.JsonDataSource();
    jsonDS.ConnectionString = @"jsondoc=https://demodata.mescius.io/northwind/odata/v1/Employees";
    jsonDS.JsonPath = "$.value[*]";
    this.DataSource = jsonDS;