ActiveReports allows you to bind reports to any type of data source, including arrays. You can create a report without setting its data source, then load the data into the control at run time. Use the ReportStart event to set up your data source, the ReportEnd event to close it, the DataInitialize event to create your fields collection, and the FetchData event to populate it.
Use the ReportStart event to connect the report to a data source
- Double-click in the gray area below the report to create an event-handling method for the 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 examples show what the code for the method looks like.
Add using or Imports statements for System.Data and System.Data.OleDb.
To create a GetDatabasePath method in Visual Basic.NET
| Visual Basic.NET code. Paste JUST ABOVE the ReportStart event. | Copy Code |
|---|---|
Private Function getDatabasePath() As String
Dim regKey As Microsoft.Win32.RegistryKey
regKey = Microsoft.Win32.Registry.LocalMachine
regKey = regKey.CreateSubKey _
("SOFTWARE\\GrapeCity\\ActiveReports 6\\SampleDB")
getDatabasePath = CType(regKey.GetValue(""), String)
End Function
Private conn As OleDbConnection
Private reader As OleDbDataReader
Private cmd As OleDbCommand | |
| Visual Basic.NET code. Paste INSIDE the ReportStart event. | Copy Code |
|---|---|
Private Sub rptYourReportName_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
| |
To create a GetDatabasePath method in C#
| C# code. Paste JUST ABOVE the ReportStart event. | Copy Code |
|---|---|
private string getDatabasePath()
{
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine;
regKey = regKey.CreateSubKey("SOFTWARE\\GrapeCity\\ActiveReports 6\\SampleDB");
return ((string)(regKey.GetValue("")));
}
private static OleDbConnection conn;
private static 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 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();
| |
Use the ReportEnd event to close the data connection
The following examples show what the code for the method looks like.
To write the code in Visual Basic.NET
- Right-click in any section of the design window of the report and select View Code.
- At the top left of the code view, click the drop-down arrow and select (rptYourReportName Events).
- At the top right of the code window, click the drop-down arrow and select ReportEnd. This creates an event-handling method for the report's ReportEnd event.
- Add code to the handler to close the data connection
| Visual Basic.NET code. Paste INSIDE the ReportEnd event. | Copy Code |
|---|---|
reader.Close() conn.Close() | |
- Click in the gray area below the report to select it.
- Click 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 report's ReportEnd event.
- Add code to the handler to close the data connection
| C# code. Paste INSIDE the ReportEnd event. | Copy Code |
|---|---|
reader.Close(); conn.Close(); | |
Use the DataInitialize event to add fields
The following examples show what the code for the method looks like.
To write the code in Visual Basic.NET
- Right-click in any section of the design view of the report and select View Code.
- At the top left of the code view, click the drop-down arrow and select (rptYourReportName Events).
- At the top right of the code window, click the drop-down arrow and select DataInitialize. This creates an event-handling method for the report's DataInitialize event.
- Add code to the handler to add fields to the report's fields collection.
| Visual Basic.NET code. Paste INSIDE the DataInitialize event. | Copy Code |
|---|---|
Fields.Add("CategoryName")
Fields.Add("ProductName")
Fields.Add("UnitsInStock")
Fields.Add("Description")
| |
- Click in the gray area below the report to select it.
- In the Properties window, click the events icon to display available events for the report.
- Double-click DataInitialize. This creates an event-handling method for the report's DataInitialize event.
- Add code to the handler to add fields to the report's fields collection.
| C# code. Paste INSIDE the DataInitialize event. | Copy Code |
|---|---|
Fields.Add("CategoryName");
Fields.Add("ProductName");
Fields.Add("UnitsInStock");
Fields.Add("Description");
| |
Use the FetchData event to populate fields
Reference the Fields collection only in the DataInitialize and FetchData events.
The following examples show what the code for the method looks like.
To write the code in Visual Basic.NET
- Right-click in any section of the design window of the report, and select View Code to display the code view for the report.
- At the top left of the code view, click the drop-down arrow and select (rptYourReportName Events).
- At the top right of the code window, click the drop-down arrow and select FetchData. This creates an event-handling method for the report's FetchData event.
- Add code to the handler to retrieve information to populate the report fields.
| Visual Basic.NET code. Paste INSIDE the FetchData event. | Copy Code |
|---|---|
Try
reader.Read()
Me.Fields("CategoryName").Value = reader("CategoryName")
Me.Fields("ProductName").Value = reader("ProductName")
Me.Fields("UnitsInStock").Value = reader("UnitsInStock")
Me.Fields("Description").Value = reader("Description")
eArgs.EOF = False
Catch ex As Exception
eArgs.EOF = True
End Try
| |
- Click in the gray area below the report to select it.
- In the Properties window, click the events icon to display available events for the report.
- Double-click FetchData. This creates an event-handling method for the report's FetchData event.
- Add code to the handler to retrieve information to populate the report fields.
| C# code. Paste INSIDE the FetchData event. | Copy Code |
|---|---|
try
{
reader.Read();
Fields["CategoryName"].Value = reader["CategoryName"].ToString();
Fields["ProductName"].Value = reader["ProductName"].ToString();
Fields["UnitsInStock"].Value = reader["UnitsInStock"].ToString();
Fields["Description"].Value = reader["Description"].ToString();
eArgs.EOF = false;
}
catch
{
eArgs.EOF = true;
}
| |
Hide All