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 supply data for the controls
- Viewing the report
- Saving the report to an XML file (RPX)
To complete the walkthrough, you must have access to the Northwind database.
A copy is located at C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\Data\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
- Open a new project in Visual Studio.
- From the Project menu, select Add New Item.
- Select ActiveReports 3.0 File and rename the file rptScript.
- Click Open (Add in Visual Studio 2005).
Adding controls to the report to contain data
To add controls to the report
- Add a GroupHeader/Footer section to your report by right-clicking on the design surface of the report and selecting Insert > Group Header/Footer.
- Make the following changes to the group header:
- Change the Name property to ghCategories
- Change the BackColor property to LightBlue
- Change the CanShrink property to True
- Change the DataField property to CategoryID
- Set the GroupKeepTogether property to All
- Set the KeepTogether property to True
- Add the following controls to the GroupHeader section and set the properties as indicated.
Control DataField Name Text Location Size Miscellaneous TextBox CategoryName txtCategoryName Category Name 0, 0 6.5, 0.2 BackColor = CadetBlue; Font Style = Bold; Font Size = 12 TextBox Description txtDescription Description 0, 0.2 6.5, 0.2 BackColor = CadetBlue Label lblProductName Product Name 0, 0.4 1, 0.2 Font Style = Bold Label lblUnitsInStock Units in Stock 5.5, 0.4 1, 0.2 Font Style = Bold; Alignment = Right - Add text box controls with the following properties to the detail section.
DataField Name Text Location Size Alignment ProductName txtProductName Product Name 0, 0 5.5, 0.19 UnitsInStock txtUnitsInStock Units in Stock 5.5, 0 1, 0.19 Right - Set the CanShrink property of the detail section to True.
- Select both of the text boxes in the detail section, right-click and choose Format Border.
- Select dark cyan in the color combo box
- Select the dotted line in the Line Styles pane
- Click the bottom edge in the Preview pane
- Click the OK button to add a dotted line to the bottom edge of the text boxes.
- Make the following changes to the group footer:
- Change the Name property to gfCategories
- Change the BackColor property to PaleGreen
- Change the CanShrink property to True
- Add the following controls to the GroupFooter section:
Control DataField Name Text Miscellaneous Location Label lblTotalLabel Total: Size = 3, 0.19; Font Style = Bold 2.5, 0 TextBox ProductName txtTotalItems Total Items SummaryType = Subtotal; SummaryFunc = Count; SummaryRunning = Group; SummaryGroup = ghCategories; Alignment = Right 5.5, 0 Label lblWhiteSpace BackColor = White; Size = 6.5, 0.2 0, 0.25
Adding scripting to supply data for the controls
To add scripting to the report
- Change the ScriptLanguage property for the report to the appropriate scripting language. The default setting is C#.
- Click the Script tab located below the report designer to access the scripting editor.
- Add the scripting code.
The following example shows what the scripting code looks like.
'VB.NET Private Shared m_reader As System.Data.OleDb.OleDbDataReader Private Shared m_cnn As System.Data.OleDb.OleDbConnection Public Sub ActiveReport_ReportStart() Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\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() 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 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") rpt.Fields("TotalLabel").Value = "Total Number of " + m_reader("CategoryName")+ " Units:" eof = False Catch eof = True End Try Return eof End Function Public Sub ActiveReport_ReportEnd() m_reader.Close m_cnn.Close End Sub //C# private static System.Data.OleDb.OleDbDataReader m_reader; private static System.Data.OleDb.OleDbConnection m_cnn; public void ActiveReport_ReportStart() { string m_cnnString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Data Dynamics\ActiveReports for .NET 3.0\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() { 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(); 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(); rpt.Fields["TotalLabel"].Value = "Total Number of " + m_reader ["CategoryName"].ToString() + " Units:"; eof = false; } catch { eof = true; } return eof; } public void ActiveReport_ReportEnd() { m_reader.Close(); m_cnn.Close(); }
Viewing the report
To view the report
- Add the ActiveReports viewer control to a Windows Form.
- Add the code needed to set the viewer document equal to the report document. See Using the ActiveReports Windows Form Viewer for help.
You can quickly view your report at design time by clicking the Preview tab at the bottom of the designer. |
Saving the report to an XML file (RPX)
To save the report
- From the Report menu, select Save Layout.
- In the Save dialog, name the file appropriately, select the location in which you wish to save it, and press Save.