ActiveReports 6 Online Help Send comments on this topic.
Basic XML-Based Reports (RPX)
ActiveReports 6 > ActiveReports User Guide > Samples and Walkthroughs > Walkthroughs > Basic XML-Based Reports (RPX)

Glossary Item Box

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.

To add controls to the report

  1. Add an ActiveReports 6 (xml-based) File to a Visual Basic project and name it rptScript.
  2. In the Solution Explorer click the rptScript.rpx item and set the Build Action property to Embedded Resource.
  3. Add the following controls to the Detail section:

    ShowDetail 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

  4. Click just below the fields to select the Detail section.
  5. In the Properties Window, set the CanShrink property to True to eliminate white space in the rendered report.

To add scripting to the report to supply data for the controls

  1. Click the Script tab located at the bottom edge of the report designer to access the scripting editor.

  2. Add the scripting code.

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.

ShowTo 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

ShowTo 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();
}

To add scripting to alternate colors in the detail section

  1. Click the Script tab located at the bottom edge of the report designer to access the scripting editor.

  2. Add the scripting code.

The following example shows what the scripting code looks like.

ShowTo 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

ShowTo 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;
    }

}

To view the report

You can quickly view your report at design time by clicking the Preview tab at the bottom of the designer.

  1. Drag the ActiveReports viewer control from the Visual Studio toolbox onto the Windows Form and set its Dock property to Fill.
  2. Double-click the title bar of the Windows Form containing the viewer to create a Form_Load event and add the code needed to load the RPX into a generic ActiveReport and display it in the viewer.

The following example shows what the code for the method looks like.

Show 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

ShowTo 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;