Walkthrough Scripting and Simple Reports

ActiveReports allows you to use scripting to permit reports saved to an XML file to contain code. By including scripting when the RPX files are saved into XML, the reports later can be loaded, run and displayed directly to the viewer control without needing to use the designer.

This walkthrough illustrates how to include scripting in a report.

This walkthrough is split into the following activities:

• Adding an ActiveReport to a Visual Studio project

• Adding controls to contain the data

• Adding scripting to set the data connection, add fields to the report's fields collection and populate the report fields

• Viewing the report

To complete the walkthrough, you must have access to the NorthWind database (NWind.mdb). When you have completed this walkthrough, you will have a report that looks similar to the following.

Condiments

Sweet and savory sa joes. ra !=h-= a-, sp'eads, and seasonngs

Aniseed Syrjp

Urita lit Stock 13

C nef 4 nlo n s Ca j n Se a so n ng

53

Chef Anton s Gumbo Mix

p

Grandms s Boysenoerry Spread

12a

l\!p rthsVP pds- Cra nberrySaupe

s

GenenSnpuyj

3S

Sj'a Ma'aooa

27

Stop b era be

113

Veg:e-spread

24

L-p u iaaji a Fiery Hpt Pe p pe r5s upe rS

Lpue ana Kot Sp:oed Üiiia

4

Or.gna-Frenifjrter grjne Sofe

32

Total Number of Co n-di merits; 12

Total Number of Co n-di merits; 12

Adding an ActiveReport to a Visual Studio project

To add an ActiveReport to your project

1. Open a new project in Visual Studio.

2. Click on Project > Add New Item.

3. Select ActiveReports file and rename the file rptScript.

4. Click Open.

Adding controls to the report to contain data To add controls to the report

1. Add a GroupHeader/Footer section to your report.

2. Make the following changes to the group header:

o Change the name to ghCategories o Change the DataField property to CategorylD o Set the GroupKeepTogether property to All o Set the KeepTogether property to True

3. Make the following change to the group footer:

o Change the name to gfCategories

4. Add the following controls to the GroupHeader section:

Location

Location

Control DataField Name Text/Caption

TextBox CategoryName txtCategoryName Category Name

TextBox Description txtDescription Description

Label (Empty string) lblProductName Product Name

Label (Empty string) lblUnitsInStock Units In Stock

5. Add the following controls to the Detail section:

Control DataField

TextBox ProductName

TextBox UnitslnStock

Name txtProductName txtUnitsInStock

Text/Caption

Product Name Units In Stock

Add the following controls to the GroupFooter section: Control DataField Name Text/Caption

Label

TotalLabel lblTotalLabel Total Label

TextBox ProductName txtTotalItems Total Items

Misc Details (Empty string) Summary Type = Subtotal SummaryFunc = Count SummaryRunning = Group SummaryGroup = ghCategories

Location

Adding scripting to add data to the controls To add scripting to the report

1. Click on the Edit Script icon on the report toolbar.

2. Add the following scripting code.

The following example shows what the scripting code looks like:

private static System.Data.OleDb.OleDbConnection m_cnn; private static System.Data.OleDb.OleDbDataReader m_reader;

private string getDatabasePath() {

Microsoft.Win32.RegistryKey regKey = Microsoft.Win3 2.Registry.LocalMachine; regKey = regKey.CreateSubKey("SOFTWARE\\Data Dynamics\\ActiveReports.NET\\Sample return (string)regKey.GetValue("");

public void ActiveReport_ReportStart() {

string m_dbPath = getDatabasePath();

string m_cnnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + m_dbPath string sqlString = "SELECT * FROM categories INNER JOIN products ON categories.c m_cnn = new System.Data.OleDb.OleDbConnection(m_cnnString);

System.Data.OleDb.OleDbCommand m_Cmd = new System.Data.OleDb.OleDbCommand(sqlStr if(m_cnn.State == System.Data.ConnectionState.Closed) {

m reader m Cmd.ExecuteReader()

public void ActiveReport_DataInitialize() {

"CategorylD"); 'CategoryName"); 'ProductName"); 'UnitsInStock"); 'Description");

rpt

Fields

Add(

rpt

Fields

Add(

rpt

Fields

Add(

rpt

Fields

Add(

rpt

Fields

Add(

rpt

Fields

Add(

public bool ActiveReport_ {

FetchData(bool eof)

catch {

m reader.Rea

d();

rpt

Fields["

CategorylD"]

Value =

rpt

Fields["

CategoryName

' ] .Value

rpt

Fields["

ProductName"

.Value

rpt

Fields["

UnitsInStock

' ] .Value

rpt

Fields["

Description"

.Value

rpt

Fields["

TotalLabel"]

Value =

eof

= false;

eof

= true;

m_reader["categories.CategoryID"].ToS1 = m_reader["CategoryName"].ToString();

m_reader["ProductName"].ToString(); = m_reader["UnitsInStock"].ToString();

m_reader["Description"].ToString(); "Total Number of " + m_reader["Categor return eof;

bool m_color;

public void Detail_Format() {

m_color =false;

rpt.Sections["Detail"].BackColor = System.Drawing.Color.DarkSeaGreen;

rpt.Sections["Detail"].BackColor = System.Drawing.Color.Transparent; m color = true;

3. Click OK to continue.

Viewing the report To view the report

1. Add the ActiveReports viewer control to a Windows Form.

2. Add the code needed to set the viewer document equal to the report document. See Using the ActiveReports WinForm Viewer for help.

Was this article helpful?

0 0

Post a comment