ActiveReports for .NET 3 Online Help Request technical support
Walkthrough: Scripting and Subreports
See Also
User Guide > Samples and Walkthroughs > Walkthroughs > Standard Edition Walkthroughs > Advanced > Scripting Walkthroughs > Walkthrough: Scripting and Subreports

Glossary Item Box

ActiveReports allows you to use scripting to permit reports saved to an XML file to contain code. By including scripting when reports 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 use scripting when creating a subreport.

This walkthrough is split up into the following activities:

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 finished this walkthrough, you will have a report that looks similar to the following.

Adding two ActiveReports to a Visual Studio project

To add two ActiveReports to a Visual Studio project

  1. Open a new project in Visual Studio.
  2. From the Project menu, select Add New Item.
  3. Select ActiveReports 3.0 File and rename the file rptMain.
  4. Click Open.
  5. From the Project menu, select Add New Item.
  6. Select ActiveReports 3.0 File and rename the file rptSub.
  7. Click Open (Add in Visual Studio 2005).
  8. Right-click the page header of rptSub and select Delete. (Page headers and footers are not displayed in subreports, so it is a good practice to delete them and save on processing.)
  9. Select rptSub in the Properties window and set the ShowParameterUI property to False. (This step prevents ActiveReports from requesting a parameter value from the user at run time.)

Connecting the subreport to a data source

To connect the subreport to a data source

  1. Click on the gray report DataSource icon in the Detail section of rptSub to open the report DataSource dialog.
  2. Click the Build button. 
  3. Select Microsoft Jet 4.0 OLE DB Provider and click Next.
  4. Click the ellipsis button to browse for the access path to the NorthWind database. Click Open once you have selected the appropriate access path.
  5. Click OK to continue.
    Parameters will be added to the query in the script of rptMain.
  6. Click OK to return to the report design surface.

Temporarily connecting the main report to a data source

To connect the main report to a data source

This step is just for convenience so that the fields list in the Report Explorer can be populated at design time.
  1. Click on the gray report DataSource icon in the Detail section of rptMain to open the report DataSource dialog.
  2. Click on Build.
  3. Select Microsoft Jet 4.0 OLE DB Provider and click Next.
  4. Click the ellipsis button to browse for the access path to the NorthWind database. Click Open once you have selected the appropriate access path.
  5. Click OK to continue.
  6. In the Query field, type "Select * from orders inner join customers on orders.customerid = customers.customerid order by CompanyName,OrderDate".
  7. Click OK to return to the report design surface.

Adding controls to each report to display the data

To add controls to rptMain

  1. Add a GroupHeader/Footer section to rptMain by right-clicking on the design surface of the report and selecting Insert > Group Header/Footer.
  2. Make the following changes to the group header:
    • Change the Name property to ghCompanies
    • Change the BackColor property to LemonChiffon
    • Change the CanShrink property to True
    • Change the DataField property to CompanyName
    • Change the GroupKeepTogether property to All
  3. In the Report Explorer, expand the Fields node, then the Bound node. Drag the following field onto ghCompanies and set the properties as indicated. 

    Field Name Miscellaneous Size Location
    CompanyName txtCompanyName1 Font Style = Bold; Font Size = 12  4, 0.2  0, 0
  4. Add a second GroupHeader/Footer section to rptMain.
  5. Make the following changes to the group header:
    • Change the Name property to ghOrders
    • Change the BackColor property to LightYellow
    • Change the CanShrink property to True
    • Change the DataField property to OrderDate
    • Change the GroupKeepTogether property to All
  6. Drag the following fields and controls onto ghOrders and set the properties as indicated.  

    Control DataField Name Text Miscellaneous Location
    TextBox OrderDate txtOrderDate1 Order Date OutputFormat = MM/dd/yy 1.13, 0
    TextBox RequiredDate txtRequiredDate1 Required Date OutputFormat = MM/dd/yy 3.5, 0
    TextBox ShippedDate txtShippedDate1 Shipped Date OutputFormat = MM/dd/yy 5.5, 0
    Label lblOrderDate Ordered: Font Style = Bold 0, 0
    Label lblRequiredDate Required: Font Style = Bold 2.5, 0
    Label lblShippedDate Shipped: Font Style = Bold 4.875, 0
  7. Change the CanShrink property of the detail section to True.
  8. Drag the following control onto the detail section and set the properties as indicated.
    Control ReportName Name Text Size Location
    Subreport C:\full project path\rptSub.rpx SubReport1 6.5, 0.2  0, 0
    The path of the RPX will be set later.

To add controls to rptSub

  1. Add a GroupHeader/Footer section to rptSub by right-clicking the design surface of the report and selecting Insert > Group Header/Footer.
  2. Make the following changes to the group header:
    • Change the Name property to ghOrderDetails
    • Change the BackColor property to LightSteelBlue
    • Change the CanShrink property to True
    • Change the DataField property to OrderID
  3. Add four label controls to ghOrderDetails and set the properties as indicated.
    Font Style Name Text Alignment Location
    Bold lblProductName Product Name 0, 0
    Bold lblQuantity Quantity Right 3.25, 0
    Bold lblUnitPrice Unit Price Right 4.38, 0
    Bold lblDiscount Discount Right 5.5, 0
  4. Add four line controls to ghOrderDetails and set the properties as indicated.
    Name X1 X2 Y1 Y2
    Line1 3.1875 3.1875 0 0.1875
    Line2 4.3125 4.3125 0 0.1875
    Line3 5.4375 5.4375 0 0.1875
    Line4 0 6.5 0.1875 0.1875
  5. Make the following changes to the detail section:
    • Change the BackColor property to Gainsboro
    • Change the CanShrink property to True
  6. Drag the following fields onto the detail section and set the properties as indicated.
    Field Name Alignment Miscellaneous Location
    ProductName txtProductName1 Right Size = 3.15, 0.19 0, 0
    Quantity txtQuantity1 Right 3.25, 0
    products.UnitPrice txtproducts_UnitPrice1 Right OutputFormat = Currency 4.4, 0
    Discount txtDiscount1 Right OutputFormat = Currency; Size = 0.95, 0.19 5.5, 0
  7. Add four line controls to the detail section and set the properties as follows (or copy and paste them from ghOrderDetails):
    Name X1 X2 Y1 Y2
    Line5 3.1875 3.1875 0 0.1875
    Line6 4.3125 4.3125 0 0.1875
    Line7 5.4375 5.4375 0 0.1875
    Line8 0 6.5 0.1875 0.1875
     

Saving the rptSub layout to RPX format

To save the layout to RPX format

  1. While in the design view of rptSub, open the Report menu and select Save Layout.
  2. Navigate to your project folder and save the report as rptSub.rpx.
  3. Click the Save button.
  4. Use Windows Explorer to navigate to rptSub.rpx and ensure that the subreport control in rptMain has the ReportName property set to the full path.

Adding the scripting code for rptMain

To add scripting to the report

  1. Change the ScriptLanguage property for the report to the appropriate scripting language. The default setting is C#.
  2. Click the Script tab located below the report designer to access the scripting editor.

  3. Add the scripting code. 

    The following example shows what the scripting code looks like.

    'VB.NET
    Private Function getDatabasePath() As String
      Dim regKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
      regKey = regKey.CreateSubKey("SOFTWARE\\Data Dynamics\\ActiveReports for .NET 3.0\\SampleDB")
      getDatabasePath = CType(regKey.GetValue(""), String)
    End Function
    
    Public Sub ActiveReport_ReportStart()
      Dim dbPath As String = getDatabasePath()
      Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + dbPath + "\\NWIND.mdb"
      Dim sqlString As String = "Select * from orders inner join customers on orders.customerid = customers.customerid order by CompanyName,OrderDate"
      Dim ds As new DataDynamics.ActiveReports.DataSources.OleDBDataSource()
      ds.ConnectionString = connString
      ds.SQL = sqlString
      rpt.DataSource = ds
    End Sub
    
    Public Sub Detail1_Format()
      Dim rptSubCtl As DataDynamics.ActiveReports.SubReport = CType(rpt.Sections("Detail1").Controls("SubReport1"), DataDynamics.ActiveReports.SubReport)
      Dim rptSub As new DataDynamics.ActiveReports.ActiveReport3()
      rptSub.LoadLayout(CType(rpt.Sections("Detail1").Controls("SubReport1"), DataDynamics.ActiveReports.SubReport).ReportName)
      Dim childDataSource As New DataDynamics.ActiveReports.DataSources.OleDBDataSource()
      childDataSource.ConnectionString = CType(rpt.DataSource, DataDynamics.ActiveReports.DataSources.OleDBDataSource).ConnectionString 
      childDataSource.SQL = "Select * from [order details] inner join products on [order details].productid = products.productid where [order details].orderid = " + orderIDString
      rptSub.DataSource = childDataSource
      rptSubCtl.Report = rptSub
    End Sub
    
    Dim orderIDString As String
    Function ActiveReport_FetchData(ByVal EOF As Boolean) As Boolean
      orderIDString = rpt.Fields("OrderID").Value
      Return EOF
    End Function
      
    //C#
    private string getDatabasePath()
    {
      Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine;
      regKey = regKey.CreateSubKey("SOFTWARE\\Data Dynamics\\ActiveReports for .NET 3.0\\SampleDB");
      return ((string)(regKey.GetValue(""))); 
    }
    
    public void ActiveReport_ReportStart()
    { 
      string dbPath = getDatabasePath();
      string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + dbPath + "\\NWIND.mdb";
      string sqlString = "Select * from orders inner join customers on orders.customerid = customers.customerid order by CompanyName,OrderDate"; 
      DataDynamics.ActiveReports.DataSources.OleDBDataSource ds = new DataDynamics.ActiveReports.DataSources.OleDBDataSource();
      ds.ConnectionString = connString;
      ds.SQL = sqlString;
      rpt.DataSource = ds;
    }
    
    public void detail_Format() 
    { 
      DataDynamics.ActiveReports.SubReport rptSubCtl = ((DataDynamics.ActiveReports.SubReport) rpt.Sections["detail"].Controls["subReport1"]);
      DataDynamics.ActiveReports.ActiveReport3 rptSub = new DataDynamics.ActiveReports.ActiveReport3(); 
      rptSub.LoadLayout(((DataDynamics.ActiveReports.SubReport)rpt.Sections["detail"].Controls["subReport1"]).ReportName); 
      DataDynamics.ActiveReports.DataSources.OleDBDataSource childDataSource = new DataDynamics.ActiveReports.DataSources.OleDBDataSource();
      childDataSource.ConnectionString = ((DataDynamics.ActiveReports.DataSources.OleDBDataSource)rpt.DataSource).ConnectionString; 
      childDataSource.SQL = "Select * from [order details] inner join products on [order details].productid = products.productid where [order details].orderid = " + orderIDString; 
      rptSub.DataSource = childDataSource; 
      rptSubCtl.Report = rptSub; 
    }
    
    string orderIDString;
    public  bool ActiveReport_FetchData(bool eof)
    {
      orderIDString = rpt.Fields["OrderID"].Value.ToString();
      return eof;
     }

Saving the report to an XML file (RPX)

To save the report

  1. From the Report menu, select Save Layout.
  2. In the Save dialog, name the file appropriately, select the location in which you wish to save it, and press Save.

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 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.

See Also

©2009. All Rights Reserved.