# Mail Merge: Inserting Merge Blocks using the DataSourceManager in C#

> This article shows how to insert merge blocks into a document using the DataSourceManager class in C#. Merge blocks are used to repeat a block of content for each data record in a data source. The DataSourceManager class provides information about the available merge blocks in a data source such as JSON.

- **Author:** Bjoern Meyer
- **Published:** 2024-10-02
- **Modified:** 2026-07-17
- **Description:** This article shows how to insert merge blocks into a document using the DataSourceManager class in C#. Merge blocks are used to repeat a block of content for each data record in a data source. The DataSourceManager class provides information about the available merge blocks in a data source such as JSON.
- **4 min read** (631 words)
- **Tags:**
  - ASP.NET
  - ASP.NET Core
  - MailMerge
  - Merge Block
- **Web URL:** https://www.textcontrol.com/blog/2024/10/02/mail-merge-inserting-merge-blocks-using-the-datasourcemanager-in-csharp/
- **LLMs URL:** https://www.textcontrol.com/blog/2024/10/02/mail-merge-inserting-merge-blocks-using-the-datasourcemanager-in-csharp/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2024/10/02/mail-merge-inserting-merge-blocks-using-the-datasourcemanager-in-csharp/llms-full.txt

---

In the context of the TX Text Control MailMerge class, a merge block is a feature that allows repeating data structures in documents to be handled during a mail merge operation.

A merge block represents a section of the document that is repeated for each data entry in a data source, such as a table or list. This is particularly useful when you want to merge multiple records into a single document, such as invoices, order lists or reports with multiple line items per document.

When the MailMerge class processes the document, the merge block is repeated for each row in the associated data source (e.g., a DataTable or JSON array).

### DataSourceManager

The out-of-the-box TX Text Control interface can be used to insert merge fields and merge blocks, and available drop-down lists are automatically populated with available block names based on a loaded data source. However, if you want to create your own UI or programmatically add merge blocks to a template, this article shows how to use the DataSourceManager to retrieve available merge blocks and their fields.

![Merge Blocks in TX Text Control](https://s1-www.textcontrol.com/assets/dist/blog/2024/10/02/a/assets/mergeblocks.webp "Merge Blocks in TX Text Control")

### Getting Merge Blocks Names

This code demonstrates how to automate the process of inserting merge blocks into a document. It loads data from a JSON file, dynamically inserts merge blocks for tables into the JSON data, and then merges this data into the document. Finally, it saves the document as a PDF.

First, by loading a data source, in this case JSON, we need to create an instance of the DataSourceManager.

```
// Load JSON data from file
 string jsonData = File.ReadAllText("tabledata.json");

 var dataSourceManager = new DataSourceManager();
 dataSourceManager.LoadJson(jsonData); // Load JSON data
```

The PossibleMergeBlockTables method returns a list of merge block names in a loaded data source. The following code snippet shows how to loop through the list of merge blocks:

```
foreach (var table in dataSourceManager.PossibleMergeBlockTables)
{
   // table.TableName
}
```

All available column names for a specific merge block can be retrieved using the Columns property. The following code snippet shows how to create a new MergeBlockInfo object and how to set the column names:

```
// Create a new MergeBlockInfo object with the table name
 var mergeBlockInfo = new MergeBlockInfo(table.TableName)
 {
     ColumnNames = table.Columns.Select(c => c.ColumnName).ToList()
 };
```

### Inserting Merge Blocks

Here is the full code that shows how to insert a merge block for each available block in a document and how to finally merge the template to create a PDF document.

```
using TXTextControl;
using TXTextControl.DocumentServer.DataSources;
using TXTextControl.DocumentServer;

using (var tx = new ServerTextControl())
{
    tx.Create();

    // Load JSON data from file
    string jsonData = File.ReadAllText("tabledata.json");

    var dataSourceManager = new DataSourceManager();
    dataSourceManager.LoadJson(jsonData); // Load JSON data

    // Add merge blocks for all tables
    foreach (var table in dataSourceManager.PossibleMergeBlockTables)
    {
        // Create a new MergeBlockInfo object with the table name
        var mergeBlockInfo = new MergeBlockInfo(table.TableName)
        {
            ColumnNames = table.Columns.Select(c => c.ColumnName).ToList()
        };

        // Create a new MergeBlockSettings object with the block type and header flag
        var mergeBlockSettings = new MergeBlockSettings(BlockTemplateType.TableRow, true);

        // Insert the merge block
        dataSourceManager.InsertMergeBlock(tx, mergeBlockInfo, mergeBlockSettings);
    }

    new MailMerge { TextComponent = tx }.MergeJsonData(jsonData);

    tx.Save("results.pdf", StreamType.AdobePDF);
}
```

When the document is merged, the merge block is repeated for each row in the data source. The resulting document is a PDF file that contains the merged data.

![Merge Blocks in TX Text Control](https://s1-www.textcontrol.com/assets/dist/blog/2024/10/02/a/assets/pdfresults.webp "Merge Blocks in TX Text Control")

### Conclusion

The DataSourceManager class provides a simple way to retrieve merge block names and their fields from a data source. This allows you to programmatically insert merge blocks into a document and merge data from a data source into the document.

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Create SignFabric Envelopes from Mail Merge Templates in .NET C#](https://www.textcontrol.com/blog/2026/06/23/create-signfabric-envelopes-from-mail-merge-templates-using-dotnet-csharp/llms.txt)
- [Use MailMerge in .NET on Linux to Generate Pixel-Perfect PDFs from DOCX Templates](https://www.textcontrol.com/blog/2025/05/27/use-mailmerge-in-dotnet-on-linux-to-generate-pixel-perfect-pdfs-from-docx-templates/llms.txt)
- [Generating Dynamic NDAs Using TX Text Control MailMerge in C# .NET](https://www.textcontrol.com/blog/2025/04/08/generating-dynamic-ndas-using-tx-text-control-mailmerge-in-csharp-dotnet/llms.txt)
- [Designing a Maintainable PDF Generation Web API in ASP.NET Core (Linux) C# with Clean Architecture and TX Text Control](https://www.textcontrol.com/blog/2025/03/27/designing-a-maintainable-pdf-generation-web-api-in-asp-net-core-linux-c-sharp-with-clean-architecture-and-tx-text-control/llms.txt)
- [Manipulating Table Cells During the MailMerge Process in .NET C#](https://www.textcontrol.com/blog/2024/12/03/manipulating-table-cells-during-the-mailmerge-process-in-net-csharp/llms.txt)
- [When to Generate Documents Server-Side Instead of Client-Side: A Focus on Data Security](https://www.textcontrol.com/blog/2024/10/04/when-to-generate-documents-server-side-instead-of-client-side-a-focus-on-data-security/llms.txt)
- [Creating Advanced Tables in PDF and DOCX Documents with C#](https://www.textcontrol.com/blog/2024/09/30/creating-advanced-tables-in-pdf-and-docx-documents-with-csharp/llms.txt)
- [Designing the Perfect Contract Template for MailMerge in C#](https://www.textcontrol.com/blog/2024/09/26/designing-the-perfect-contract-template-for-mailmerge-in-csharp/llms.txt)
- [Video Tutorial: Creating a MailMerge Template and JSON Data Structure](https://www.textcontrol.com/blog/2024/08/16/video-tutorial-creating-a-mailmerge-template-and-json-data-structure/llms.txt)
- [Getting Started Video Tutorial: How to use the MailMerge and ServerTextControl Classes in ASP.NET Core C#](https://www.textcontrol.com/blog/2024/08/05/getting-started-video-tutorial-how-to-use-the-mailmerge-and-servertextcontrol-classes-in-asp-net-core-c/llms.txt)
- [Getting Started Videos: New Text Control YouTube Channel](https://www.textcontrol.com/blog/2024/08/02/getting-started-videos-new-text-control-youtube-channel/llms.txt)
- [Best Practices for Mail Merge and Form Field Processing in ASP.NET Core C# Applications](https://www.textcontrol.com/blog/2024/07/30/best-practices-for-mail-merge-and-form-field-processing-in-asp-net-core-csharp-applications/llms.txt)
- [Advantages of Flow Type Layout Reporting vs. Banded Reporting or PDF Template Engines in .NET C#](https://www.textcontrol.com/blog/2024/07/29/advantages-of-flow-type-layout-reporting-vs-banded-reporting-or-pdf-template-engines-in-net-c-sharp/llms.txt)
- [Designing a MailMerge Web API Endpoint with ASP.NET Core in C#](https://www.textcontrol.com/blog/2024/07/12/designing-a-mailmerge-web-api-endpoint-with-asp-net-core-in-c-sharp/llms.txt)
- [Enhancing Documents with QR Codes and Barcodes in .NET C#: A Comprehensive Guide](https://www.textcontrol.com/blog/2024/07/11/enhancing-documents-with-qr-codes-and-barcodes-in-net-csharp-a-comprehensive-guide/llms.txt)
- [Document Automation 101: Leveraging TX Text Control for Business Efficiency in .NET C# Applications](https://www.textcontrol.com/blog/2024/07/09/document-automation-101-leveraging-tx-text-control-for-business-efficiency-in-net-c-applications/llms.txt)
- [Merging Templates with MailMerge with Different Merge Field Settings in C#](https://www.textcontrol.com/blog/2023/12/16/merging-templates-with-mailmerge-with-different-merge-field-settings/llms.txt)
- [How to Mail Merge MS Word DOCX Documents in ASP.NET Core C#](https://www.textcontrol.com/blog/2023/10/16/how-to-mail-merge-ms-word-docx-documents-in-aspnet-core-csharp/llms.txt)
- [Mail Merge: Conditional Data Shaping in Merge Blocks](https://www.textcontrol.com/blog/2023/05/03/mail-merge-conditional-data-shaping-in-merge-blocks/llms.txt)
- [MailMerge: Working with Image Placeholders](https://www.textcontrol.com/blog/2022/12/22/mailmerge-working-with-image-placeholders/llms.txt)
- [Getting Started: ServerTextControl and MailMerge with ASP.NET Core](https://www.textcontrol.com/blog/2022/09/01/getting-started-servertextcontrol-and-mailmerge-with-aspnet-core/llms.txt)
- [Adding SVG Watermarks to Documents](https://www.textcontrol.com/blog/2022/01/28/adding-svg-watermarks-to-documents/llms.txt)
- [Using MailMerge in ASP.NET Core 6 Web Applications](https://www.textcontrol.com/blog/2022/01/27/using-mailmerge-in-aspnet-core-6-web-applications/llms.txt)
- [Silicon Valley, Here We Come!](https://www.textcontrol.com/blog/2026/08/10/silicon-valley-here-we-come/llms.txt)
- [Against the Trend: Why We Still Believe in Transparent, Perpetual Software Licensing. And Why You Should Too](https://www.textcontrol.com/blog/2026/08/06/against-the-trend-transparent-perpetual-software-licensing/llms.txt)
