ActiveReports allows you to change the data source of a report at run time. This walkthrough illustrates how to change the data source at run time.
This walkthrough is split up into the following activities:
When you complete this walkthrough you get a layout that looks similar to the following at design time and at run time.
To add an ActiveReport to the Visual Studio project
See Adding an ActiveReport to a Project for information on adding different report layouts.
To connect the report to a data source
SQL Query |
Copy Code
|
---|---|
SELECT * FROM Products |
To create a layout for the report
TextBox1 (ProductID)
Property Name | Property Value |
---|---|
Location | 0, 0 in |
Size | 0.5, 0.2 in |
TextBox2 (ProductName)
Property Name | Property Value |
---|---|
Location | 0.6, 0 in |
Size | 2.8, 0.2 in |
TextBox3 (UnitsInStock)
Property Name | Property Value |
---|---|
Location | 3.5, 0 in |
Size | 0.5, 0.2 in |
Alignment | Right |
TextBox4 (UnitsOnOrder)
Property Name | Property Value |
---|---|
Location | 4.1, 0 in |
Size | 0.5, 0.2 in |
Alignment | Right |
TextBox5 (UnitPrice)
Property Name | Property Value |
---|---|
Location | 4.7, 0 in |
Size | 0.9, 0.2 in |
Alignment | Right |
OutputFormat | Currency |
To change the data source at run time
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 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 connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "C:\Users\[YourUserName]\Documents\GrapeCity Samples\ActiveReports 11\Data\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 connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @"C:\Users\[YourUserName]\Documents\GrapeCity Samples\ActiveReports 11\Data\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
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(); |
To view the report
OR