Glossary Item Box

Samples | Walkthroughs | Scripting

See Also ActiveReports for .NET 2 Online Help Send feedback to Data Dynamics

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 into the viewer control without the use of the designer.

This walkthrough illustrates how to include scripting in a report.

This walkthrough is split into the following activities:

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.

Adding an ActiveReport to a Visual Studio project

To add an ActiveReport to your project

  1. Open a new project in Visual Studio.
  2. From the Project menu, select Add New Item...
  3. Select ActiveReports File and name the file rptScript.
  4. Click Open.

Adding controls to the report to contain data

To add controls to the report

  1. Right-click anywhere on the report and select Insert > Group Header/Footer.
  2. Make the following changes to the group header:
    • Change the name to ghCategories
    • Change the DataField property to CategoryID
    • Set the GroupKeepTogether property to All
    • Set the Height property to 0.75 in
    • Set the KeepTogether property to True
  3. Make the following change to the group footer:
  4. Make the following change to the Detail section:
  5. Add the following controls to the GroupHeader section:

    Control DataField Name Text/Caption Location
    TextBox CategoryName txtCategoryName Category Name 0, 0
    TextBox Description txtDescription Description 0, 0.25
    Label (Empty string) lblProductName Product Name 0, 0.5
    Label (Empty string) lblUnitsInStock Units in Stock 5, 0.5
  6. Add the following controls to the Detail section:

    Control DataField Name Text/Caption Location
    TextBox ProductName txtProductName Product Name 0, 0
    TextBox UnitsInStock txtUnitsInStock Units in Stock 5, 0
  7. Add the following controls to the GroupFooter section:

    Control DataField Name Text/Caption Misc. Details Location
    Label TotalLabel lblTotalLabel Total Label

    Width = 2.75

    2.25, 0
    TextBox ProductName txtTotalItems Total Items Summary Type = Subtotal

    SummaryFunc = Count

    SummaryRunning = Group

    SummaryGroup = ghCategories

    5, 0

Adding scripting to add data to the controls

To add scripting to the report

  1. Below the report designer, you will find two tabs. The one on the left is for the Script view, and the one on the right is for the Design view. Click the View Script tab.

  2. Add the scripting code.

    The following example shows what the scripting code looks like.

    //C#
    private System.Data.OleDb.OleDbConnection conn;
    private System.Data.OleDb.OleDbDataReader reader;
    private bool moreRows = true;
    
    private string GetDatabasePath()
    {
    	Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine;
    	key = key.OpenSubKey( "SOFTWARE\\Data Dynamics\\ActiveReports for .NET 2.0\\
    		SampleDB" );
    	
    	if( key == null )
    		return "";
    		
    	return (string) key.GetValue("");
    }
    
    public void ActiveReport_ReportStart()
    {
    	string dbPath = GetDatabasePath();
    	string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath 
    		+ "\\NWind.mdb";
    	string sql = "SELECT Categories.CategoryID, CategoryName, ProductName, 
    		UnitsInStock, Description FROM Categories INNER JOIN Products ON 
    		Categories.CategoryID = Products.CategoryID ORDER BY Products.CategoryID, 
    		Products.ProductID";
    	
    	conn = new System.Data.OleDb.OleDbConnection( connStr );
    	System.Data.OleDb.OleDbCommand cmd = new 
    		System.Data.OleDb.OleDbCommand( sql, conn );
    	
    	conn.Open();
    	reader = cmd.ExecuteReader();
    }
    
    public void ActiveReport_DataInitialize()
    {
    	rpt.Fields.Add( "CategoryID" );
    	rpt.Fields.Add( "CategoryName" );
    	rpt.Fields.Add( "ProductName" );
    	rpt.Fields.Add( "UnitsInStock" );
    	rpt.Fields.Add( "Description" );
    	rpt.Fields.Add( "TotalLabel" );
    	
    	moreRows = reader.Read();
    }
    
    public bool ActiveReport_FetchData(bool eof)
    {
    	if( moreRows )
    	{
    		rpt.Fields["CategoryID"].Value = reader["CategoryID"]
    			.ToString();
    		rpt.Fields["CategoryName"].Value = reader["CategoryName"].ToString();
    		rpt.Fields["ProductName"].Value = reader["ProductName"].ToString();
    		rpt.Fields["UnitsInStock"].Value = reader["UnitsInStock"].ToString();
    		rpt.Fields["Description"].Value = reader["Description"].ToString();
    		rpt.Fields["TotalLabel"].Value = "Total Number of " + 
    			reader["CategoryName"].ToString() + ":";
    	}
    	
    	moreRows = reader.Read();
    	
    	eof = !moreRows;
    	
    	return eof;
    }
    
    bool color = false;
    public void Detail_Format()
    {
    	if( color )
    		rpt.Sections["Detail"].BackColor = System.Drawing.Color.DarkSeaGreen;
    	else
    		rpt.Sections["Detail"].BackColor = System.Drawing.Color.Transparent;
    		
    	color = !color;
    }
    
    public void ActiveReport_ReportEnd()
    {
    	reader.Close();
    	conn.Close();
    }

Adding a script reference and viewing the report

To add a script reference and view the report

  1. Add the ActiveReports viewer control to a Windows Form and set its Dock property to Fill.
  2. Add the code needed to set the viewer document equal to the report document and to make System.Data.dll available to the script:

To write the code for the viewer in Visual Basic

To write the code for the viewer in C#

Samples | Walkthroughs | Scripting

 

 


Copyright © 2004-2005 Data Dynamics, Ltd. All rights reserved.