# Bind a Section Report to Data Source at Run Time

Learn how to bind a section report to a data source at run time.

## Content



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.
    
    ```vbnet
    Dim conn As System.Data.OleDb.OleDbConnection
    Dim reader As System.Data.OleDb.OleDbDataReader
    ```
    
    ```vbnet
    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
    ```
    
    ```csharp
    private static System.Data.OleDb.OleDbConnection conn;
    private static System.Data.OleDb.OleDbDataReader reader;
    ```
    
    ```csharp
    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.
    
    ```vbnet
    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.
    
    ```csharp
    reader.Close();
    conn.Close();
    ```
    

See the [UnboundData](https://github.com/activereports/Samples19/tree/main/DataBinding/Section/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.
    
    ```vbnet
    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
    ```
    
    ```csharp
    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;
    ```
