ActiveReports 11
Run-Time Data Sources
ActiveReports 11 > ActiveReports User Guide > Samples and Walkthroughs > Walkthroughs > Section Report Walkthroughs > Data > Run-Time Data Sources

ActiveReports allows you to change the data source of a report at run time. This walkthrough illustrates how to change the data source at run time.

This walkthrough is split up into the following activities:

Note: This walkthrough uses the Northwind database. By default, in ActiveReports, the Northwind.mdb file is located at [User Documents folder]\GrapeCity Samples\ActiveReports 11\Data\NWIND.mdb.

When you complete this walkthrough you get a layout that looks similar to the following at design time and at run time.

Design-Time Layout

Run-Time Layout

To add an ActiveReport to the Visual Studio project

  1. Create a new Visual Studio project.
  2. From the Project menu, select Add New Item.
  3. In the Add New Item dialog that appears, select ActiveReports 11 Section Report (code-based) and in the Name field, rename the file as rptModifyDS.
  4. Click the Add button to open a new section report in the designer.

See Adding an ActiveReport to a Project for information on adding different report layouts.

To connect the report to a data source

Tip: Even if you will change the data source at run time, setting a design time data source allows you to drag fields onto the report from the Report Explorer.
  1. On the detail section band, click the Data Source Icon.
  2. In the Report Data Source dialog that appears, on the OLE DB tab, next to Connection String, click the Build button.
  3. In the Data Link Properties window that appears, select Microsoft Jet 4.0 OLE DB Provider and click the Next button to move to the Connection tab.
  4. Click the ellipsis (...) button to browse to your database, for example the NWind.mdb sample database. Click Open once you have selected the appropriate database path.
  5. Click the Test Connection button to see if you have successfully connected to the database.
  6. Click OK to close the Data Link Properties window and return to the Report Data Source dialog. Notice that the Connection String field gets filled automatically.
  7. In the Query field on the OLE DB tab, enter the following SQL query.
    SQL Query
    Copy Code
    SELECT * FROM Products
  8. Click OK to save the data source and return to the report design surface.

To create a layout for the report

  1. On the design surface of the report, select the detail section and in the Properties window, set the CanShrink property to True.
  2. In the Report Explorer, expand the Fields node, then the Bound node. Drag the following fields onto the detail section and in the Properties window, set the following properties.

TextBox1 (ProductID)

Property Name Property Value
Location 0, 0 in
Size 0.5, 0.2 in

TextBox2 (ProductName)

Property Name Property Value
Location 0.6, 0 in
Size 2.8, 0.2 in

TextBox3 (UnitsInStock)

Property Name Property Value
Location 3.5, 0 in
Size 0.5, 0.2 in
Alignment Right

TextBox4 (UnitsOnOrder)

Property Name Property Value
Location 4.1, 0 in
Size 0.5, 0.2 in
Alignment Right

TextBox5 (UnitPrice)

Property Name Property Value
Location 4.7, 0 in
Size 0.9, 0.2 in
Alignment Right
OutputFormat Currency

To change the data source at run time

To change the data source at run time

  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.

To write the code in Visual Basic.NET

The following example shows what the code for the method looks like.

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=" + "C:\Users\[YourUserName]\Documents\GrapeCity Samples\ActiveReports 11\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

To write the code in C#

The following example shows what the code for the method looks like.

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=" + @"C:\Users\[YourUserName]\Documents\GrapeCity Samples\ActiveReports 11\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;

To close the data connection

To write the code in 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.

The following example shows what the code for the method looks like.

Visual Basic.NET code. Paste INSIDE the ReportEnd event.
Copy Code
reader.Close()
conn.Close()

To write the code in C#

  1. Click in the gray area below rptModifyDS to select the report.
  2. Click the events icon in the Properties Window 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.

The following example shows what the code for the method looks like.

C# code. Paste INSIDE the ReportEnd event.
Copy Code
reader.Close();
conn.Close();

To view the report

OR

See Also

Samples