Products Technologies Demo Docs Blog Support Company

MailMerge: Creating Separate Documents from Multiple Data Records

By default, the Merge method of the MailMerge class, merges all records into a single document. The second parameter append specifies whether the single documents should be merged into one document or splitted into separate documents. public void Merge(System.Data.DataTable mergeData, bool append); When connected to a TextComponent such as the visual TXTextControl.TextControl, the resulting document is loaded into the connected Text Control. In case the Merge method has been used with the…

MailMerge: Creating Separate Documents from Multiple Data Records

By default, the Merge method of the MailMerge class, merges all records into a single document.

The second parameter append specifies whether the single documents should be merged into one document or splitted into separate documents.

public void Merge(System.Data.DataTable mergeData, bool append);

When connected to a TextComponent such as the visual TXTextControl.TextControl, the resulting document is loaded into the connected Text Control. In case the Merge method has been used with the append parameter false, only the last created document is loaded back into the Text Control.

In order to access each single created document, the DataRowMerged event can be used to save the return value of this event. The event arguments DataRowMergedEventArgs return the merged document in the internal TX Text Control format that can be accessed using the MergedRow property.

The following sample shows how to use the event by merging the following simple template consisting of two merge fields:

MailMerge: Creating separate documents from multiple data records

As a data source, a simple DataSet with one DataTable is used. Before the Merge method is called with the append parameter set to false, the DataRowMerged event is attached:

DataTable dt = new DataTable();
dt.Columns.Add("company");
dt.Columns.Add("name");

dt.Rows.Add(new string[] { "Text Control", "Peter Jackson" });
dt.Rows.Add(new string[] { "Microsoft", "Sue Williamson" });
dt.Rows.Add(new string[] { "Google", "Peter Gustavsson" });

mailMerge1.LoadTemplate(Server.MapPath("template.docx"),
    TXTextControl.DocumentServer.FileFormat.WordprocessingML);

mailMerge1.DataRowMerged += mailMerge1_DataRowMerged;
mailMerge1.Merge(dt, false);

In the DataRowMerged event, each merged record is loaded into a temporary ServerTextControl and saved as HTML which is then sent back to the browser using HttpResponse:

void mailMerge1_DataRowMerged(object sender,
    TXTextControl.DocumentServer.MailMerge.DataRowMergedEventArgs e)
{
    string htmlDocument = "";

    using (TXTextControl.ServerTextControl tx =
        new TXTextControl.ServerTextControl())
    {
        tx.Create();
        tx.Load(e.MergedRow,
            TXTextControl.BinaryStreamType.InternalUnicodeFormat);
        tx.Save(out htmlDocument, TXTextControl.StringStreamType.HTMLFormat);
    }

    Response.Write(htmlDocument);
    Response.Write("<hr />");
}

The resulting HTML shows the 3 merged records divided by an horizontal rule:

MailMerge: Creating separate documents from multiple data records

Try this on your own and download the sample project for Visual Studio 2012. At least, a trial version of TX Text Control .NET Server X11 is required.

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

.NET C# PDF Generation Techniques: Which Method is Right for Your Project?

This article explains the distinct advantage TX Text Control offers with its true WYSIWYG (What You See Is What You Get) template system, where templates are designed visually, similar to…


ASP.NETReportingASP.NET Core

Best Practices for Mail Merge and Form Field Processing in ASP.NET Core C#…

This article provides best practices for mail merge and form field processing in ASP.NET Core C# applications using TX Text Control .NET Server. It shows how to use the Document Editor to create…


ASP.NETReportingASP.NET Core

Advantages of Flow Type Layout Reporting vs. Banded Reporting or PDF…

This article shows the advantages of flow type layout reporting compared to banded reporting or populating PDF placeholders in .NET C#. It explains the differences and the benefits of using a flow…


ActiveXASP.NETReporting

TX Text Control 32.0 Has Been Released

We are pleased to announce the immediate availability of TX Text Control 32.0 for all platforms including ASP.NET, Windows Forms, WPF and ActiveX.


AngularJavaScriptReporting

Getting Started: Angular Document Editor Attributes Explained

This article explains the possible attributes that can be used when initializing the TX Text Control Document Editor for Angular.