ActiveReports 8 > 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.
Note: These steps assume that the data path is defined in the following registry key. |
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 |
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\\ComponentOne\\ActiveReports\\v8") 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\\ComponentOne\\ActiveReports\\v8"); return ((string)(regKey.GetValue(""))); } |
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 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(); |