Hey, tell me please.
I want to get some information from database, create docx with tables inside and do mailmerge option.
In my case it doesn’t work. Could you tell me where the bug is?
static List<Table1Row> GetTable1Data(string connStr)
{
var rows = new List<Table1Row>();
using (var conn = new SqlConnection(connStr))
using (var cmd = new SqlCommand("SELECT [ATTORNEY_NAME] AS Attorney,[ATTORNEY_RATE] AS Rate,[RANK] AS Title " +
" FROM [Custom].[dbo].[ Rates] ",
conn))
{
conn.Open();
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
rows.Add(new Table1Row
{
Attorney = reader["Attorney"].ToString(),
Rate = reader["Rate"].ToString(),
Title = reader["Title"].ToString()
});
}
}
}
return rows;
}
public class Table1Row
{
public string Attorney { get; set; }
public string Rate { get; set; }
public string Title { get; set; }
}
public class SourceData
{
public List<Table1Row> Table1 { get; set; }
}
Used this code to populate the collection.
var source = new SourceData
{
Table1 = GetTable1Data(connectionString)
};
And used this code to display it in the document:
Paragraph parrrr = new Paragraph(dc);
Section_ExhibitB.Blocks.Add(parrrr);
var table = new Table(dc, 2, 2);
table.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage);
table.Rows[0].Cells[0].Blocks.Add(
new Paragraph(dc,
new Field(dc, FieldType.MergeField, "RangeStart:Table1"),
new Field(dc, FieldType.MergeField, "Attorney")));
table.Rows[0].Cells[1].Blocks.Add(
new Paragraph(dc,
new Field(dc, FieldType.MergeField, "Rate"))
{
ParagraphFormat =
{
LeftIndentation = 350,
//RightIndentation = 150,
//SpecialIndentation = 30
}
});
table.Rows[1].Cells[1].Blocks.Add(
new Paragraph(dc,
new Field(dc, FieldType.MergeField, "Title"),
new Field(dc, FieldType.MergeField, "RangeEnd:Table1"))
{
ParagraphFormat =
{
//LeftIndentation = 0,
RightIndentation = 0,
//SpecialIndentation = 30
}
});
Section_ExhibitB.Blocks.Add(table);
dc.MailMerge.Execute(source);