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.
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. |
SQL Query |
Copy Code |
---|---|
SELECT * FROM Products |
Detail 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 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 |
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; |
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 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(); |