This walkthrough illustrates the fundamentals of using the DataInitialize and FetchData events to set up an unbound report.
This walkthrough is split into the following activities:
- Adding an ActiveReport to a Visual Studio project
- Adding code to connect the report to a data source
- Adding controls to contain the data
- Using the DataInitialize event to add fields to the report's fields collection
- Using the FetchData event to populate the report fields
- Using 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.
When you have completed this walkthrough, you will have a report that looks similar to the following.
Adding an ActiveReport to a Visual Studio 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 rptUnbound.
- Click Open.
Adding code to connect the report to a data source
To write the code in Visual Basic or C#
- Double-click in the gray area below rptUnbound to create an event-handling method for rptUnbound's ReportStart event. Add code to the handler to:
- Set the database path (place this code above the ReportStart event)
- Set the data source connection string
- Set the data source SQL query
- Open the connection to create the DataReader
The following example shows what the code for the method looks like.
Add using or Imports statements for System.Data and System.Data.OleDb. 'Visual Basic Private Function getDatabasePath() As String Dim regKey As Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine regKey = regKey.CreateSubKey _ ("SOFTWARE\\Data Dynamics\\ActiveReports for .NET 3.0\\SampleDB") getDatabasePath = CType(regKey.GetValue(""), String) End Function Private conn As OleDbConnection Private reader As OleDbDataReader Private cmd As OleDbCommand Private Sub rptUnbound_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 OleDbConnection(connString) cmd = New OleDbCommand("SELECT * FROM categories INNER JOIN products ON _ categories.categoryid = products.categoryid ORDER BY products.categoryid, products.productid", conn) conn.Open() reader = cmd.ExecuteReader() End Sub //C# private string getDatabasePath() { Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine; regKey = regKey.CreateSubKey("SOFTWARE\\Data Dynamics\\ActiveReports for .NET 3.0\\SampleDB"); return ((string)(regKey.GetValue(""))); } private static OleDbConnection conn; private static OleDbDataReader reader; private void rptUnbound_ReportStart(object sender, System.EventArgs eArgs) { string dbPath = getDatabasePath(); string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + dbPath + "\\NWIND.mdb"; conn = new OleDbConnection(connString); OleDbCommand cmd = new OleDbCommand("SELECT * FROM categories INNER JOIN products ON categories.categoryid = products.categoryid ORDER BY products.categoryid, products.productid", conn); conn.Open(); reader = cmd.ExecuteReader(); }
Adding controls to the report to contain data
To add controls to the report
Add the following controls from the ActiveReports 3.0 toolbox to the Detail section of rptUnbound:
Control | DataField | Name | Text | Location |
---|---|---|---|---|
TextBox | ProductName | txtProductName | Product Name | 0.0625, 0.375 |
TextBox | UnitsInStock | txtUnitsInStock | Units In Stock | 4.75, 0.375 |
TextBox | Description | txtDescription | Description | 1.8125, 0.6875 |
TextBox | CategoryName | txtCategoryName | Category Name | 3.125, 0.375 |
Label | NA | lblCategoryDescription | Category Description | 0.0625, 0.6875 |
Label | NA | lblUnitsInStock | Units In Stock | 4.75, 0.0625 |
Label | NA | lblCategoryName | Category Name | 3.125, 0.0625 |
Label | NA | lblProductName | Product Name | 3.125, 0.375 |
Line | NA | line1 | NA | X1 = 0
X2 = 6.5 Y1 = 1.0625 Y2 = 1.0625 |
Additionally, you can set the BackColor property of the label controls to an opaque color to make your report easier to read. |
Using the DataInitialize event to add fields
To write the code in Visual Basic
- Right-click in any section of the design window of rptUnbound, and click on View Code to display the code view for the report. At the top left of the code view for rptUnbound, click the drop-down arrow and select (rptUnbound Events). At the top right of the code window, click the drop-down arrow and select DataInitialize. This creates an event-handling method for rptUnbound's DataInitialize event. Add code to the handler to:
- Add fields to the report's fields collection
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 DataInitialize. This creates an event-handling method for rptUnbound's DataInitialize event. Add code to the handler to:
- Add fields to the report's fields collection
The following example shows what the code for the method looks like.
'Visual Basic
Private Sub rptUnbound_DataInitialize(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles MyBase.DataInitialize
Fields.Add("CategoryName")
Fields.Add("ProductName")
Fields.Add("UnitsInStock")
Fields.Add("Description")
End Sub//C#
private void rptUnbound_DataInitialize(object sender, System.EventArgs eArgs)
{
Fields.Add("CategoryName");
Fields.Add("ProductName");
Fields.Add("UnitsInStock");
Fields.Add("Description");
}
Using the FetchData event to populate the report fields
To write the code in Visual Basic
- Right-click in any section of the design window of rptUnbound, and click on View Code to display the code view for the report. At the top left of the code view for rptUnbound, click the drop-down arrow and select (rptUnbound Events). At the top right of the code window, click the drop-down arrow and select FetchData. This creates an event-handling method for rptUnbound's FetchData event. Add code to the handler to:
- Retrieve information to populate the report fields
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 FetchData. This creates an event-handling method for rptUnbound's FetchData event. Add code to the handler to:
- Retrieve information to populate the report fields
The following example shows what the code for the method looks like.
'Visual Basic
Private Sub rptUnbound_FetchData(ByVal sender As Object, ByVal eArgs As DataDynamics._
ActiveReports.ActiveReport.FetchEventArgs) Handles MyBase.FetchData
Try
reader.Read()
Me.Fields("CategoryName").Value = dr("CategoryName")
Me.Fields("ProductName").Value = dr("ProductName")
Me.Fields("UnitsInStock").Value = dr("UnitsInStock")
Me.Fields("Description").Value = dr("Description")
eArgs.EOF = False
Catch ex As Exception
eArgs.EOF = True
End Try
End Sub//C#
private void rptUnbound_FetchData(object sender, DataDynamics.ActiveReports.ActiveReport3.
FetchEventArgs eArgs)
{
try
{
reader.Read();
Fields["CategoryName"].Value = dr["CategoryName"].ToString();
Fields["ProductName"].Value = dr["ProductName"].ToString();
Fields["UnitsInStock"].Value = dr["UnitsInStock"].ToString();
Fields["Description"].Value = dr["Description"].ToString();
eArgs.EOF = false;
}
catch
{
eArgs.EOF = true;
}
}
The DataInitialize and FetchData events are the only events in which the Fields collection should ever be referenced. |
Using the ReportEnd event to close the data connection
To write the code in Visual Basic
- Right-click in any section of the design window of rptUnbound, and click on View Code to display the code view for the report. At the top left of the code view for rptUnbound, click the drop-down arrow and select (rptUnbound Events). At the top right of the code window, click the drop-down arrow and select ReportEnd. This creates an event-handling method for rptUnbound's ReportEnd event. 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 rptUnbound's ReportEnd event. 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 rptUnbound_ReportEnd(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles MyBase.ReportEnd reader.Close() conn.Close() End Sub //C# private void rptUnbound_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. |