Products Technologies Demo Docs Blog Support Company

MailMerge: Merging Fields from Child Tables or Related Objects

The Merge method of MailMerge accepts a DataTable as a parameter and the MergeObjects method accepts an IEnumerable object such as a List as the data source. This DataTable or object acts as the master table for the merge process. MergeField names without a prefix in the template are matched to this table and doesn't require a prefix. The rows of the master table are used to create the document instances of the template. In other words: The number of resulting documents in a merge process…

MailMerge: Merging Fields from Child Tables or Related Objects

The Merge method of MailMerge accepts a DataTable as a parameter and the MergeObjects method accepts an IEnumerable object such as a List as the data source. This DataTable or object acts as the master table for the merge process.

MergeField names without a prefix in the template are matched to this table and doesn't require a prefix. The rows of the master table are used to create the document instances of the template. In other words: The number of resulting documents in a merge process depends on the number of records in the master table.

All relations between child tables and the master table is defined as a prefix in the merge field name concatenated with a dot:

ChildTable[.ChildTable].ColumnName

Our sample database has the following structure:

MailMerge: Merging fields from child tables or related objects

In case the master table is Sales_SalesOrderHeader, the merge field values for MergeFields without a prefix are coming from this table. If MergeFields have a prefix, the values are coming from related tables such as Sales_SalesOrderDetail:

Sales_SalesOrdcerDetail.ProductID

The master table is the base table for the complete report. Inside of merge blocks, the master table for this block is the merge block table. All child tables of this block table can be used inside of the merge block with the same notation. In other words: Each merge block as a new master table and relations are based on the block master table.

As a business object, the above DataSet would look similar to the illustrated classes below:

MailMerge: Merging fields from child tables or related objects

The class SalesOrderHeader has a property of type SalesOrderDetail which acts like a child table in our datasource context.

class SalesOrderHeader
{
    public int RevisionNumber { get; set; }
    public DateTime OrderDate { get; set; }
    public SalesOrderDetail OrderDetail { get; set; }
}

class SalesOrderDetail
{
    public List<Production_Product> Products { get; set; }
    public Customer Customer { get; set; }
}

class Production_Product
{
    public string Name { get; set; }
    public string ProductNumber { get; set; }
}

class Customer
{
    public int CustomerID { get; set; }
    public string Name { get; set; }
    public int SalesPersonID { get; set; }
}

The merge field name syntax is following the same rules as for DataTables. The relation between related objects is defined as a prefix in the merge field name concatenated with a dot.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Reporting

The Text Control Reporting Framework combines powerful reporting features with an easy-to-use, MS Word compatible word processor. Users can create documents and templates using ordinary Microsoft Word skills. The Reporting Framework is included in all .NET based TX Text Control products including ASP.NET, Windows Forms and WPF.

See Reporting products

Related Posts

ASP.NETReportingMail Merge

MailMerge Class Settings Explained

This article explains the different settings of the MailMerge class and how and when they should be used.


ReportingWindows FormsExcel

Merge Excel Documents into MailMerge Templates using IncludeText Fields

This sample shows how to use IncludeText fields to merge Excel documents into templates using MailMerge.


ASP.NETReportingWindows Forms

MailMerge: Conditional Table Cell Colors using Filter Instructions

The MailMerge class provides an extensible framework to inject custom logic to the merge process. This sample shows how to implement conditional table cell colors.


ASP.NETReportingWindows Forms

MailMerge: Using Filters to Remove Unwanted Rows

Data in merge blocks can be filtered and sorted. This article explains how to use filters to remove unwanted lines.


ReportingWindows FormsMail Merge

MailMerge: Conditional Rendering of Merge Blocks

Merge blocks can be used to render content conditionally. This article explains different ways to control this.