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.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.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.Save(out htmlDocument, TXTextControl.StringStreamType.HTMLFormat);

    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 for ASP.NET X11 is required.