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.
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(); |
The following examples show what the code for the method looks like.
To write the code in Visual Basic.NET
Visual Basic.NET code. Paste INSIDE the ReportEnd event. |
Copy Code |
---|---|
reader.Close() conn.Close() |
To write the code in C#
C# code. Paste INSIDE the ReportEnd event. |
Copy Code |
---|---|
reader.Close(); conn.Close(); |
The following examples show what the code for the method looks like.
To write the code in Visual Basic.NET
Visual Basic.NET code. Paste INSIDE the DataInitialize event. |
Copy Code |
---|---|
Fields.Add("CategoryName") Fields.Add("ProductName") Fields.Add("UnitsInStock") Fields.Add("Description") |
To write the code in C#
C# code. Paste INSIDE the DataInitialize event. |
Copy Code |
---|---|
Fields.Add("CategoryName"); Fields.Add("ProductName"); Fields.Add("UnitsInStock"); Fields.Add("Description"); |
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
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 |
To write the code in C#
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; } |