Page tree

Versions Compared

Key

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

...

Here is some example code that demonstrates the usage of SetRepeatBlock with a DataTable that has multiple rows. This code inserts data into merge fields "FirstName" and "LastName" enclosed in the repeat block named "BookMarkName".

Private Sub
Code Block
c#c#
titleC#
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);
}
Code Block
vb.netvb.net
titleVB.NET
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

...


<vbnet}

Code Sample: Mail Merge using Repeat Blocks

...