ActiveReports allows you to modify your data source at run time. Below is sample code that you can use to connect a report to the Nwind.mdb sample database at run time.
To 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 System Imports Microsoft.Win32 |
Visual Basic.NET code. Paste INSIDE the report class and hit ENTER. |
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\\GrapeCity\\ActiveReports 6\\SampleDB") getDatabasePath = CType(regKey.GetValue(""), String) |
To 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; 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 INSIDE the getDatabasePath function. |
Copy Code |
---|---|
RegistryKey regKey = Registry.LocalMachine; regKey = regKey.CreateSubKey("SOFTWARE\\GrapeCity\\ActiveReports 6\\SampleDB"); 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 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 |
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 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(); |