ActiveReports 10
Modify Data Sources at Run Time
ActiveReports 10 > ActiveReports User Guide > How To > Section Report How To > Work with Data in Section Reports > Modify Data Sources at Run Time

In a section report, you can modify your data source at run time. Follow the steps below to connect your report to the NWind.mdb sample database at run time.

To find the database path

Note: These steps assume that the data path is defined in the following registry key.
  1. Right-click the design surface, and select View Code to display the code view for the report.
  2. Add the following code to the report to access the sample database path from the registry.

To write the code in Visual Basic

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

Visual Basic.NET code. Paste below the Imports GrapeCity.ActiveReports statement at the top of the code view.
Copy Code
Imports System 
Imports Microsoft.Win32
Visual Basic.NET code. Paste inside the report class.
Copy Code
Private Function getDatabasePath() As String
End Function

This creates a function for getDatabasePath.

Visual Basic.NET code. Paste inside the getDatabasePath function.
Copy Code
Dim regKey As RegistryKey
regKey = Registry.LocalMachine
regKey = regKey.CreateSubKey("SOFTWARE\\GrapeCity\\ActiveReports\\v10")
getDatabasePath = CType(regKey.GetValue(""), String)

To write the code in C#

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

C# code. Paste below the using GrapCity.ActiveReports statement at the top of the code view.
Copy Code
using Microsoft.Win32;
using System;
C# code. Paste inside the report class and hit Enter.
Copy Code
private string getDatabasePath()

This creates a function for getDatabasePath.

C# code. Paste BELOW the getDatabasePath function.
Copy Code
{
RegistryKey regKey = Registry.LocalMachine;
regKey = regKey.CreateSubKey("SOFTWARE\\GrapeCity\\ActiveReports\\v10");
return ((string)(regKey.GetValue("")));
}

To change the data source at run time

  1. Double-click the gray area outside the design surface to create an event-handling method for the ReportStart event.
  2. Add the following 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 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

To write the code in C#

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

C# code. Paste 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

  1. Right-click the gray area outside the design surface and select Properties.
  2. In the Properties Window that appears, click the Events button. A list of report events appear.
  3. Select the ReportEnd event and double click to create an event-handling method.
  4. Add the following code to the handler to close the data connection.

To write the code in Visual Basic

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#

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