Same tablename at two places for mail merge

Hi
I have a document where I want to pull the address at one place and company name at another place from the same table. My template looks like this

«TableStart:ClientDetails»
«company»
«TableEnd:ClientDetails »
«TableStart:ContactDetails»
«contact»
«TableEnd:ContactDetails»
«TableStart:ClientDetails»
«address1» «address2»
«city»
«TableEnd:ClientDetails»

Is it possible to do something like this? Basically pulling some information from a table at one place and other information at other place in the document.
Please suggest.
Thanks

Yes, it is possible. You can load load your tables into a DataSet and do a single merge with regions. Or you can do several merges with each table separately. Each merge with regions will fill all regions that correspond to the table in the data source. Just make sure that merge region name and the name of the DataTable match.

Hi Aspose Team,
I’m facing the same issue.
Execpt, that i can`t merge more than one table.
I’m receiving data from an SQL stored procedure as a sqlDataReader.
Is it what cause the problem?
How can I merge more than one table in this case?

Hi
Thanks for your inquiry. I think you can run ExecuteWithRegions method several times. For example see the following code.

DataTable table = new DataTable("table");
table.Columns.Add("Field1");
table.Columns.Add("Field2");
table.Columns.Add("Field3");
table.Columns.Add("Field4");
for (int i = 0; i < 10; i++)
{
    DataRow row = table.NewRow();
    row[0] = "test1";
    row[1] = "test2";
    row[2] = "test3";
    row[3] = "test4";
    table.Rows.Add(row);
}
Document doc = new Document(@"391_78691_nicson\in.doc");
string[] names = doc.MailMerge.GetFieldNames();
for (int i = 0; i < names.Length; i++)
{
    if (names[i].ToLower() == "tablestart:" + table.TableName.ToLower())
    {
        doc.MailMerge.ExecuteWithRegions(table);
    }
}
doc.Save(@"391_78691_nicson\out.doc");

I use the following template.

«TableStart:table»«Field1»«TableEnd:table»
«TableStart:table»«Field2»«TableEnd:table»
«TableStart:table»«Field3»«TableEnd:table»
«TableStart:table»«Field4»«TableEnd:table»

Best regards.

Hi,
Thanks for the code, this code works for me too.
But if I replace the DataTable, by a foward only SqlDataReader, only one of the region get merge.
I guess, I should change my code to use a DataTable, instead of s DataReader.

Hi
I think that you can use the following code to achieve this.

public void TestMailMerge_78691()
{
    string connectionString = "server=web1;database=LS;uid=sa;pwd=password;";
    System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString);
    System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand("SELECT * FROM Account", connection);
    DataTable table = new DataTable();
    connection.Open();
    System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader();
    table = GetTable(reader);
    table.TableName = "Account";
    connection.Close();
    Document doc = new Document(@"265_78691_nicson\in.doc");
    string[] names = doc.MailMerge.GetFieldNames();
    for (int i = 0; i < names.Length; i++)
    {
        if (names[i].ToLower() == "tablestart:" + table.TableName.ToLower())
        {
            doc.MailMerge.ExecuteWithRegions(table);
        }
    }
    doc.Save(@"265_78691_nicson\out.doc");
}
public System.Data.DataTable GetTable(System.Data.SqlClient.SqlDataReader reader)
{
    System.Data.DataTable table = reader.GetSchemaTable();
    System.Data.DataTable dt = new System.Data.DataTable();
    System.Data.DataColumn dc;
    System.Data.DataRow row;
    System.Collections.ArrayList al = new System.Collections.ArrayList();
    for (int i = 0; i < table.Rows.Count; i++)
    {
        dc = new System.Data.DataColumn();
        if (!dt.Columns.Contains(table.Rows[i]["ColumnName"].ToString()))
        {
            dc.ColumnName = table.Rows[i]["ColumnName"].ToString();
            dc.Unique = Convert.ToBoolean(table.Rows[i]["IsUnique"]);
            dc.AllowDBNull = Convert.ToBoolean(table.Rows[i]["AllowDBNull"]);
            dc.ReadOnly = Convert.ToBoolean(table.Rows[i]["IsReadOnly"]);
            al.Add(dc.ColumnName);
            dt.Columns.Add(dc);
        }
    }
    while (reader.Read())
    {
        row = dt.NewRow();
        for (int i = 0; i < al.Count; i++)
        {
            row[((System.String)al[i])] = reader[(System.String)al[i]];
        }
        dt.Rows.Add(row);
    }
    return dt;
}

Best regards.

You are too strong.
Is there something that can resist you?
I’m totally convince nothing.
Thanks a lot, your code works perfect.