Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Csharp
void GenerateDocument(int employeeID)
{
     //--- Query the database
     DataTable dt = new DataTable();
     using(SqlConnection conn = new SqlConnection(connString))
     {
          string sql =
               "SELECT FirstName, LastName FROM Employee";
          SqlDataAdapter adpt = new SqlDataAdapter(sql, conn);
          adpt.Fill(dt);
     }

     //--- Use a DataTable as the data source
     WordTemplate wt = new WordTemplate();
     wt.Open(templatePath);
     wt.SetRepeatBlock(dt, "BookMarkName");
     wt.Process();
     wt.Save(Page.Response, "RepeatBlockOutput.doc", false);
}
Vbnet

Private Sub GenerateDocument(ByVal employeeID As Integer)
'--- Query the database
Dim dt As New DataTable()
Dim conn As New SqlConnection(connString)
Dim sql As String = _
"SELECT FirstName, LastName FROM Employee""
Try
Dim adpt As New SqlDataAdapter(sql, conn)
adpt.Fill(dt)
Finally
If Not conn Is Nothing Then
conn.Dispose()
End If
End Try

'--- Use a DataTable as the data source
Dim wt As New WordTemplate()
wt.Open(templatePath)
wt.SetRepeatBlock(dt, "BookMarkName")
wt.Process()
wt.Save(Page.Response, "RepeatBlockOutput.doc", False)
End Sub

...

Code Sample: Mail Merge using Repeat Blocks

...