ActiveReports 6 Online Help
Run Time Data Sources
See Also Send comments on this topic.
ActiveReports 6 > ActiveReports User Guide > Samples and Walkthroughs > Walkthroughs > Run Time or Ad Hoc Reporting > Run Time Data Sources

Glossary Item Box

ActiveReports allows you to change the data source of a report at run time. This walkthrough illustrates how to find the location of the sample database file on the user's computer and connect the report to it at run time.

This walkthrough is split up into the following activities:

Tip: For basic steps like adding a report to a Visual Studio project and viewing a report, please see the Basic Data Bound Reports walkthrough.

To complete the walkthrough, you must have access to the Northwind database.
A copy is located at C:\Program Files\GrapeCity\ActiveReports 6\Data\NWIND.MDB (on a 64-bit Windows operating system, a copy is located in C:\Program Files (x86)\GrapeCity\ActiveReports 6\Data\NWIND.MDB).

When you have finished this walkthrough, you will have a report that looks similar to the following.

To connect the report to a design time 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. Add an ActiveReport 6 (code-based) File to a Visual Studio project, and rename the file rptModifyDS.
  2. Click the gray report DataSource icon in the Detail section to open the report DataSource dialog.
  3. On the OLE DB tab, click the Build button.
  4. Select Microsoft Jet 4.0 OLE DB Provider and click Next.
  5. Click the ellipsis button to browse for the access path to the Northwind database. Click Open once you have selected the appropriate access path.
  6. Click OK to continue.
  7. In the Query field, paste the following SQL query.
    SQL Query Copy Code
    SELECT * FROM Products
  8. Click OK to return to the report design surface.

To add controls to the report

  1. Click the detail section to select it, and in the Properties Window, set the CanShrink property to True.
  2. In the Report Explorer, expand the Fields node, then the Bound node.
  3. Drag the following fields onto the detail section of the report and set the properties of each textbox as indicated.

ShowDetail section fields

Field Size Location Miscellaneous
ProductID 0.5, 0.2 in 0, 0 in
ProductName 2.8, 0.2 in 0.6, 0 in
UnitsInStock 0.5, 0.2 in 3.5, 0 in Alignment = Right
UnitsOnOrder 0.5, 0.2 in 4.1, 0 in Alignment = Right
UnitPrice 0.9, 0.2 in 4.7, 0 in OutputFormat = Currency
Alignment = Right

To find the database path

  1. Right-click in any section of the design window of rptModifyDS, and select View Code to display the code view for the report.
  2. Add code to the report to get the sample database path from the registry.

ShowTo write the code in Visual Basic

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

Visual Basic.NET code. Paste JUST BELOW the Imports DataDynamics.ActiveReports statements at the top of the code view. Copy Code
Imports Microsoft.Win32
Visual Basic.NET code. Paste INSIDE the report class. Copy Code
Private Function getDatabasePath() As String
   Dim regKey As RegistryKey
   regKey = Registry.LocalMachine
   regKey = regKey.CreateSubKey("SOFTWARE\\GrapeCity\\ActiveReports 6\\SampleDB")
   getDatabasePath = CType(regKey.GetValue(""), String)
End Function

ShowTo write the code in C#

C# code. Paste JUST BELOW the using DataDynamics.ActiveReports; statements at the top of the code view. Copy Code
using Microsoft.Win32;
C# code. Paste INSIDE the report class. Copy Code
private string getDatabasePath()
{
   RegistryKey regKey = Registry.LocalMachine;
   regKey = regKey.CreateSubKey("SOFTWARE\\GrapeCity\\ActiveReports 6\\SampleDB");
   return ((string)(regKey.GetValue("")));
}

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.

ShowTo 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 dbPath As String = getDatabasePath()
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath + "\\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

ShowTo 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 dbPath = getDatabasePath();
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath + "\\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

ShowTo 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()

ShowTo 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();

See Also