ActiveReports allows you to change the data source of a report at run time based on the location of the sample database file on the user's computer.
This walkthrough illustrates how to set up a report to set a report's data source at run time.
This walkthrough is split up into the following activities:
- Adding an ActiveReport to a Visual Studio project
- Connecting the report to a data source
- Adding controls to the report to contain data
- Adding code to set the database path
- Adding code to the ReportStart event to change the data source at run time
- Adding code to the ReportEnd event to close the data connection
- Viewing the report
To complete the walkthrough, you must have access to the Northwind database.
A copy is located at C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\Data\NWIND.MDB.
Adding an ActiveReport to your project
To add an ActiveReport to your project
- Open a new project in Visual Studio.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptModifyDS.
- Click Open.
Connecting the data source to a database
To connect the data source to a database
- Click the gray report DataSource icon in the Detail section to open the report DataSource dialog.
- Click Build.
- Select "Microsoft Jet 4.0 OLE DB Provider" and click Next.
- Click the ellipsis button to browse for the access path to the Northwind database. Click Open once you have selected the appropriate access path.
- Click OK to continue.
- In the Query field, type "Select * from products".
- Click OK to return to the report design surface.
Adding controls to contain data
To add controls to the report
- In the Report Explorer, expand the Fields node, then the Bound node. Drag the following fields onto the detail section and set the following properties of each textbox as indicated.
Field Text Location OutputFormat ProductID Product ID 0, 0.063 ProductName Product Name 1.125, 0.063 UnitsInStock Units In Stock 2.25, 0.063 UnitsOnOrder Units On Order 3.375, 0.063 UnitPrice Unit Price 4.5, 0.063 Currency
Adding code to set the database path
To write the code in Visual Basic
- Right-click in any section of the design window of rptModifyDS, and click on View Code to display the code view for the report. Underneath the "Imports System" statement at the top of the code view, add "Imports Microsoft.Win32;". Beneath "Report Designer Generated Code" type the following: "Private Function getDatabasePath() As String" and hit "Enter." This creates a function for getDatabasePath.
To write the code in C#
- Right-click in any section of the design window of rptModifyDS and click on View Code to display the code view for the report. Underneath the "using System;" statement at the top of the code view, add "using Microsoft.Win32;". Beneath "Report Designer Generated Code" type the following: "private string getDatabasePath()". This creates a function for getDatabasePath.
The following example shows what the code for the function looks like.
'Visual Basic Private Function getDatabasePath() As String Dim regKey As RegistryKey regKey = Registry.LocalMachine regKey = regKey.CreateSubKey("SOFTWARE\\Data Dynamics\\ActiveReports for .NET 3.0\\SampleDB") getDatabasePath = CType(regKey.GetValue(""), String) End Function //C# private string getDatabasePath() { RegistryKey regKey = Registry.LocalMachine; regKey = regKey.CreateSubKey("SOFTWARE\\Data Dynamics\\ActiveReports for .NET 3.0\\SampleDB"); return ((string)(regKey.GetValue(""))); }
Adding code to the ReportStart event
To write the code in Visual Basic or C#
- Double-click in the gray area below rptModifyDS to create an event-handling method for the ReportStart event of rptModifyDS. Add code to the handler to:
- Change the data source at run time
The following example shows what the code for the method looks like.
'Visual Basic Dim conn As System.Data.OleDb.OleDbConnection Dim reader As System.Data.OleDb.OleDbDataReader Private Sub rptModifyDS_ReportStart(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles MyBase.ReportStart 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 End Sub //C# private static System.Data.OleDb.OleDbConnection conn; private static System.Data.OleDb.OleDbDataReader reader; private void rptModifyDS_ReportStart(object sender, System.EventArgs eArgs) { 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; }
Adding code to the ReportEnd event
To write the code in Visual Basic
- Right-click in any section of the design window of rptModifyDS, and click on View Code to display the code view for the report. At the top left of the code view for rptModifyDS, click the drop-down arrow and select (rptModifyDS Events). At the top right of the code window, click the drop-down arrow and select ReportEnd. This creates an event-handling method for ReportEnd event of rptModifyDS. Add code to the handler to:
- Close the data connection
To write the code in C#
- Click in the gray area below rptUnbound to select the report. Click on the events icon in the Properties window to display available events for the report. Double-click ReportEnd. This creates an event-handling method for the ReportEnd event of rptModifyDS. Add code to the handler to:
- Close the data connection
The following example shows what the code for the method looks like.
'Visual Basic Private Sub rptModifyDS_ReportEnd(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles MyBase.ReportEnd reader.Close() conn.Close() End Sub //C# private void rptModifyDS_ReportEnd(object sender, System.EventArgs eArgs) { reader.Close(); conn.Close(); }
- Close the data connection
Viewing the report
To view the report
- Add the ActiveReports viewer control to a Windows Form.
- Add the code needed to set the viewer document equal to the report document. See Using the ActiveReports Windows Form Viewer for help.
You can quickly view your report at design time by clicking the Preview tab at the bottom of the designer. |