Glossary Item Box
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.
To add an ActiveReport to your project
To add controls to the report
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 |
Control | DataField | Name | Text/Caption | Location |
---|---|---|---|---|
TextBox | ProductName | txtProductName | Product Name | 0, 0 |
TextBox | UnitsInStock | txtUnitsInStock | Units in Stock | 5, 0 |
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 |
To add scripting to the report
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(); }
To add a script reference and view the report
To write the code for the viewer in Visual Basic
To write the code for the viewer in C#
The following example shows what the code for the method looks like.
' Visual Basic Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ MyBase.Load Dim rpt As New rptScript() rpt.AddScriptReference( "System.Data.dll" ) rpt.Run() Viewer1.Document = rpt.Document End Sub
//C# private void Form1_Load(object sender, System.EventArgs e) { rptMain rpt = new rptScript(); rpt.AddScriptReference( "System.Data.dll" ); rpt.Run(); this.viewer1.Document = rpt.Document; }
See Also |
Samples | Walkthroughs | Scripting
Copyright © 2004-2005 Data Dynamics, Ltd. All rights reserved.