| ActiveReports 8 > ActiveReports User Guide > Samples and Walkthroughs > Walkthroughs > Section Report Walkthroughs > Script > Script for Simple Reports |
ActiveReports allows you to use scripting to embed code in reports saved 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 using the designer. This walkthrough illustrates how to include scripting in a simple report.
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. |
![]() |
Note: This walkthrough uses the the Northwind database. By default, in ActiveReports, the NWind.mdb file is located in [User Documents folder]\ComponentOne Samples\ActiveReports 8\Data folder. |
When you have finished this walkthrough, you will have a report that looks similar to the following at design time and at runtime.
To add an ActiveReport to the Visual Studio project
See Adding an ActiveReport to a Project for information on adding different report layouts.
To connect the report to a data source
![]() |
Note: The following steps are just for convenience so that the fields list in the Report Explorer can be populated at design time. |
| SQL Query |
Copy Code
|
|---|---|
SELECT * FROM categories INNER JOIN products ON categories.categoryid =
products.categoryid ORDER BY products.categoryid, products.productid
|
|
To create a layout for the report
| Property Name | Property Value |
|---|---|
| BackColor | LightBlue |
| CanShrink | True |
| DataField | CategoryName |
| GroupKeepTogether | All |
| KeepTogether | True |
TextBox1
| Property Name | Property Value |
|---|---|
| DataField | CategoryName |
| Location | 0, 0 in |
| Size | 6.5, 0.2 in |
| BackColor | CadetBlue |
| Font Bold | True |
| Font Size | 12 |
TextBox2
| Property Name | Property Value |
|---|---|
| DataField | Description |
| Location | 0, 0.2 in |
| Size | 6.5, 0.2 in |
| BackColor | CadetBlue |
Label1
| Property Name | Property Value |
|---|---|
| Text | Product Name |
| Location | 0, 0.4 in |
| Size | 1, 0.2 in |
| Font Bold | True |
Label2
| Property Name | Property Value |
|---|---|
| Text | Units in Stock |
| Location | 5.5, 0.4 in |
| Size | 1, 0.2 in |
| Font Bold | True |
| Alignment | Right |
TextBox1
| Property Name | Property Value |
|---|---|
| DataField | ProductName |
| Location | 0, 0 in |
| Size | 5.5, 0.2 in |
TextBox2
| Property Name | Property Value |
|---|---|
| DataField | UnitsInStock |
| Location | 5.5, 0 in |
| Size | 1, 0.2 in |
| Alignment | Right |
| Property Name | Property Value |
|---|---|
| BackColor | PaleGreen |
| CanShrink | True |
TextBox1
| Property Name | Property Value |
|---|---|
| DataField | TotalLabel |
| Location | 2.5, 0 in |
| Size | 3, 0.2 in |
| Font Bold | True |
TextBox2
| Property Name | Property Value |
|---|---|
| DataField | ProductName |
| Location | 5.5, 0 in |
| SummaryType | Subtotal |
| SummaryFunc | Count |
| SummaryRunning | Group |
| SummaryGroup | GroupHeader1 |
| Alignment | Right |
Label1
| Property Name | Property Value | ||
|---|---|---|---|
| Location | 0, 0.25 in | ||
| Size | 6.5, 0.2 in | ||
| BackColor | White (creates white space after the subtotal) | ||
| Text |
|
To add scripting to the report to supply data for the controls
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_reader As System.Data.OleDb.OleDbDataReader
Private Shared m_cnn As System.Data.OleDb.OleDbConnection
Public Sub ActiveReport_ReportStart()
'Set up a data connection for the report
rpt.DataSource = ""
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\[User Folder]\Documents\ComponentOne Samples\ActiveReports 8\Data\NWIND.mdb"
Dim sqlString As String = "SELECT * FROM categories INNER JOIN products ON categories.categoryid = products.categoryid ORDER BY products.categoryid, products.productid"
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
m_reader = m_Cmd.ExecuteReader
End Sub
Public Sub ActiveReport_DataInitialize()
'Add data fields to the report
rpt.Fields.Add("CategoryID")
rpt.Fields.Add("CategoryName")
rpt.Fields.Add("ProductName")
rpt.Fields.Add("UnitsInStock")
rpt.Fields.Add("Description")
rpt.Fields.Add("TotalLabel")
End Sub
Public Function ActiveReport_FetchData(ByVal eof As Boolean) As Boolean
Try
m_reader.Read
'Populated the fields with data from the data reader
rpt.Fields("CategoryID").Value = m_reader("categories.CategoryID")
rpt.Fields("CategoryName").Value = m_reader("CategoryName")
rpt.Fields("ProductName").Value = m_reader("ProductName")
rpt.Fields("UnitsInStock").Value = m_reader("UnitsInStock")
rpt.Fields("Description").Value = m_reader("Description")
'Concatenate static text with data
rpt.Fields("TotalLabel").Value = "Total Number of " + m_reader("CategoryName")+ " Products:"
eof = False
Catch
'If the end of the data file has been reached, tell the FetchData function
eof = True
End Try
Return eof
End Function
Public Sub ActiveReport_ReportEnd()
'Close the data reader and connection
m_reader.Close
m_cnn.Close
End Sub
|
|
To write the script in C#.
| C# script. Paste in the script editor window. |
Copy Code
|
|---|---|
//C#
private static System.Data.OleDb.OleDbDataReader m_reader;
private static System.Data.OleDb.OleDbConnection m_cnn;
public void ActiveReport_ReportStart()
{
//Set up a data connection for the report
rpt.DataSource = "";
string m_cnnString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\[User Folder]\Documents\ComponentOne Samples\ActiveReports 8\Data\NWIND.mdb";
string sqlString = "SELECT * FROM categories INNER JOIN products ON categories.categoryid = products.categoryid ORDER BY products.categoryid, products.productid";
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();
}
m_reader = m_Cmd.ExecuteReader();
}
public void ActiveReport_DataInitialize()
{
//Add data fields to the report
rpt.Fields.Add("CategoryID");
rpt.Fields.Add("CategoryName");
rpt.Fields.Add("ProductName");
rpt.Fields.Add("UnitsInStock");
rpt.Fields.Add("Description");
rpt.Fields.Add("TotalLabel");
}
public bool ActiveReport_FetchData(bool eof)
{
try
{
m_reader.Read();
//Populated the fields with data from the data reader
rpt.Fields["CategoryID"].Value = m_reader["categories.CategoryID"].ToString();
rpt.Fields["CategoryName"].Value = m_reader["CategoryName"].ToString();
rpt.Fields["ProductName"].Value = m_reader["ProductName"].ToString();
rpt.Fields["UnitsInStock"].Value = m_reader["UnitsInStock"].ToString();
rpt.Fields["Description"].Value = m_reader["Description"].ToString();
//Concatenate static text with data
rpt.Fields["TotalLabel"].Value = "Total Number of " + m_reader["CategoryName"].ToString() + " Products:";
eof = false;
}
catch
{
//If the end of the data file has been reached, tell the FetchData function
eof = true;
}
return eof;
}
public void ActiveReport_ReportEnd()
{
//Close the data reader and connection
m_reader.Close();
m_cnn.Close();
}
|
|
To save the report to an XML-based RPX file
To view the report
OR
Show All
Hide All
