ActiveReports 6 allows you to create reports with embedded script and save them to the XML-based RPX file format. By embedding script in reports saved as RPX files, you can later load, run, and display reports directly in the viewer control without rebuilding the application. This walkthrough illustrates how to create a simple report, using the XML-based report template.
This walkthrough is split into the following activities:
Tip: For basic steps like adding a report to a Visual Studio project and viewing a report, please see the Basic Data Bound Reports walkthrough. |
To complete the walkthrough, you must have access to the Northwind database. A copy is located at C:\Program Files\GrapeCity\ActiveReports 6\Data\NWIND.MDB (on a 64-bit Windows operating system, a copy is located in C:\Program Files (x86)\GrapeCity\ActiveReports 6\Data\NWIND.MDB).
When you have finished this walkthrough, you will have a report that looks similar to the following.
Detail section fields
Control | Name | DataField | Location | Size |
---|---|---|---|---|
TextBox | txtProductName | ProductName | 0, 0 in | 2.3, 0.2 in |
TextBox | txtQuantityPerUnit | QuantityPerUnit | 2.4, 0 in | 1.5, 0.2 in |
TextBox | txtUnitsInStock | UnitsInStock | 4, 0 in | 1, 0.2 in |
The following example shows what the scripting code looks like.
Warning: Do not access the Fields collection outside the DataInitialize and FetchData events. Accessing the Fields collection outside of these events is not supported, and has unpredictable results. |
To write the script in Visual Basic.NET
Visual Basic.NET script. Paste in the script editor window. |
Copy Code |
---|---|
Private Shared m_cnn As System.Data.OleDb.OleDbConnection Public Sub ActiveReport_ReportStart() 'Set up a data connection for the report Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\GrapeCity\ActiveReports 6\Data\NWIND.mdb" Dim sqlString As String = "SELECT * FROM products" m_cnn = new System.Data.OleDb.OleDbConnection(connString) Dim m_Cmd As System.Data.OleDb.OleDbCommand = new System.Data.OleDb.OleDbCommand(sqlString, m_cnn) If m_cnn.State = System.Data.ConnectionState.Closed Then m_cnn.Open End If rpt.DataSource = m_Cmd.ExecuteReader End Sub Public Sub ActiveReport_ReportEnd() 'Close the data reader and connection m_cnn.Close End Sub |
To write the script in C#
C# script. Paste in the script editor window. |
Copy Code |
---|---|
private static System.Data.OleDb.OleDbConnection m_cnn; public void ActiveReport_ReportStart() { //Set up a data connection for the report string m_cnnString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\GrapeCity\ActiveReports 6\Data\NWIND.mdb"; string sqlString = "SELECT * FROM products"; m_cnn = new System.Data.OleDb.OleDbConnection(m_cnnString); System.Data.OleDb.OleDbCommand m_Cmd = new System.Data.OleDb.OleDbCommand(sqlString, m_cnn); if(m_cnn.State == System.Data.ConnectionState.Closed) { m_cnn.Open(); } rpt.DataSource = m_Cmd.ExecuteReader(); } public void ActiveReport_ReportEnd() { //Close the data reader and connection m_cnn.Close(); } |
The following example shows what the scripting code looks like.
To write the script in Visual Basic.NET
Visual Basic.NET script. Paste in the script editor window. |
Copy Code |
---|---|
Dim b as boolean = true Sub Detail1_Format if b then Me.Detail1.BackColor = Color.AliceBlue b= false else me.Detail1.BackColor = Color.Cyan b = true End If End Sub |
To write the script in C#
C# script. Paste in the script editor window. |
Copy Code |
---|---|
bool color = true; public void Detail1_Format() { if(color) { this.Detail1.BackColor = System.Drawing.Color.AliceBlue; color = false; } else { this.Detail1.BackColor = System.Drawing.Color.Cyan; color = true; } } |
You can quickly view your report at design time by clicking the Preview tab at the bottom of the designer.
The following example shows what the code for the method looks like.
To write the script in Visual Basic.NET
Visual Basic.NET script. Paste INSIDE the form load event. |
Copy Code |
---|---|
Dim asm As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly() Dim st As IO.Stream = asm.GetManifestResourceStream(asm.GetName().Name + ".rptScript.rpx") Dim report As New DataDynamics.ActiveReports.ActiveReport Using reader As New System.Xml.XmlTextReader(st) report.LoadLayout(reader) End Using report.Run() Viewer1.Document = report.Document |
To write the script in C#
C# script. Paste INSIDE the form load event. |
Copy Code |
---|---|
System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly(); System.IO.Stream st = asm.GetManifestResourceStream(asm.GetName().Name + ".rptScript.rpx"); DataDynamics.ActiveReports.ActiveReport report = new DataDynamics.ActiveReports.ActiveReport(); using (System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(st)) { report.LoadLayout(reader); } report.Run(); viewer1.Document = report.Document; |