Aspose Words Table is printing duplicate

I have issues with Aspose Words Table. After reading several documentation and I figured this might be correct way as attached a sample word template. It works but sometimes I noticed table is printing twice when if is used twice I believed. Below is the code.

public DataTable RetrieveCombinedCrscPercentages(int caseId)
{
    DecisionController decision = new DecisionController();
    DataSet ds = decision.RetrieveCalculatedCombinedPercentages(caseId);
    DataTable dt = ds.Tables[0].Copy();

    dt.Columns.Add("vasrdDisaPct", typeof(string));
    dt.Columns.Add("vasrdDisaPct2", typeof(string));
    dt.Columns.Add("crsceffdates", typeof(string));
    dt.Columns.Add("crsceffdates2", typeof(string));
    dt.Columns.Add("vasrdstartdate", typeof(string));
    dt.Columns.Add("vasrdenddate", typeof(string));
    dt.Columns.Add("VaeffstartDt", typeof(string));

    DataTable pctDT = decision.RetrieveSlidingPercentageList(caseId, true);


    // If we want to perform two consecutive mail merges on one document while taking data from two tables
    // related to each other in any way, we can separate the mail merges with regions.
    // Normally, MERGEFIELDs contain the name of a column of a mail merge data source.
    // Instead, we can use "TableStart:" and "TableEnd:" prefixes to begin/end a mail merge region.
    // Each region will belong to a table with a name that matches the string immediately after the prefix's colon.
    // These regions are separate for unrelated data, while they can be nested for hierarchical data.

    // foreach (DataRow dr in dt.Rows)
    foreach (DataRow dr in dt.Rows)
    {
        DateTime startDate = (dr["StartDate"].ToString() == String.Empty) | dr["StartDate"].ToString().Equals(DateTime.MinValue.ToString()) ?
                        DateTime.MaxValue : DateTime.Parse(String.Format("{0:yyyy/mm/dd}", dr["StartDate"].ToString()));
        DateTime startDate2 = (dr["StartDate"].ToString() == String.Empty) | dr["StartDate"].ToString().Equals(DateTime.MinValue.ToString()) ?
                    DateTime.MaxValue : DateTime.Parse(String.Format("{0:mm/yyyy}", dr["StartDate"].ToString()));
        DateTime endDate = (dr["EndDate"].ToString() == String.Empty) | dr["EndDate"].ToString().Equals(DateTime.MinValue.ToString()) ?
            DateTime.MaxValue : DateTime.Parse(String.Format("{0:yyyy/mm/dd}", dr["EndDate"].ToString()));

        DataRow[] rows = pctDT.Select("StartDate ='" + startDate.ToString() + "'");

        // Modify some row level properties.
        int ctr = 0;
        foreach (DataRow row in rows)
        {
            dr["vasrdDisaPct"] += row["VASRD_1"].ToString() + " " + row["VASRD_2"].ToString() + " " + row["VASRD_3"].ToString() + " (" + row["PERCENTAGE"].ToString() + "%) ";

            dr["vasrdstartdate"] = Util.FormatNotificationDate(dr["StartDate"]);
            dr["vasrdendDate"] = Util.FormatNotificationDate(dr["EndDate"]);

            if (dr["vasrdendDate"].ToString() != "")
            {
                dr["crsceffdates"] = dr["CombDisaPctRnd"].ToString() + "% from " + dr["vasrdstartdate"] + " to " + dr["vasrdenddate"] +
                " for Code(s) " + dr["vasrdDisaPct"];
            }
            else
            {
                dr["crsceffdates"] = dr["CombDisaPctRnd"].ToString() + "% from " + dr["vasrdstartdate"] + " for Code(s) " + dr["vasrdDisaPct"];
            }
            ctr++;
        }
    }
    // dt.TableName = "CombinedCRSCPercentages";
    return dt;
}

Output
a. The effective date of this CRSC is:

80% from 01 Oct 2013 to 31 Oct 2014 for Code(s) 7541 (60%) 7913 (20%) 8520 (10%) 8520 (10%)

Combined % CRSC Effective Date
80 01 Oct 2013

90% from 01 Nov 2014 for Code(s) 7005 (60%)

Combined % CRSC Effective Date
90 01 Nov 2014

But want it as below

80% from 01 Oct 2013 to 31 Oct 2014 for Code(s) 7541 (60%) 7913 (20%) 8520 (10%) 8520 (10%)

90% from 01 Nov 2014 for Code(s) 7005 (60%)

Combined % CRSC Effective Date
80 01 Oct 2013
90 01 Nov 2014

I have highlighted the section that is causing problem. A CH61 APPROVAL (KL) Output.docx (85.8 KB)
A CH61 APPROVAL (KL) Template.docx (85.7 KB)

@vlbuch23 It looks like both attached documents are output documents, none of them is a template. I suspect the problem on your side occurs because whole table is enclosed into TableStart/TableEnd region. In this case everything between TableStart/TableEnd fields is repeated. Please see the following simple template and code: incorrect.docx (12.5 KB) incorrect_out.docx (9.9 KB)

DataTable dt = new DataTable("mytable");
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
dt.Rows.Add("Alexey", "Noskov");
dt.Rows.Add("Victor", "Lawrence");

Document doc = new Document(@"C:\Temp\incorrect.docx");
doc.MailMerge.ExecuteWithRegions(dt);
doc.Save(@"C:\Temp\incorrect_out.docx");

As you can see whole table is repeated for each record in the data table. To repeat only the row for each record you should put TableStart at the beginning of the first cell in the row and TableEnd at the end of the last cell of this row. See the attached template and output document.

Document doc = new Document(@"C:\Temp\correct.docx");
doc.MailMerge.ExecuteWithRegions(dt);
doc.Save(@"C:\Temp\correct_out.docx");

correct.docx (12.6 KB) correct_out.docx (9.8 KB)

Ok understood. One of the other problems I am facing is when I use the table twice it does not work. Only first time it executes. I am attaching the template I did not earlier. A CH61 APPROVAL (KL) (5).docx (88.1 KB)

@vlbuch23 If you need to merge duplicated regions in your template, you should set MergeDuplicateRegions property. By default only the first region is filled with data.

Yes I reviewing that. So with that enabled to true I should be able to uses same table twice in word?

@vlbuch23 Yes, if this option is enabled all regions with the same name will be filled with data.