
Having loaded the sample program from the Step1 directory, start the program and use the Template / Load... command to load a sample file. The file already contains various text fields which are to be replaced by database items.

Select Database / Browse Database to open the Address Database form, which lets you select a record from a sample address database and merge it with the document:

When you click the Merge button, the contents of the database fields are copied to the corresponding text fields in the document:

You can select and insert different records by repeatedly clicking on the Next and Merge buttons.
The database used is an XML file which contains a few address records. Data is loaded into the Address Database form when the form is first loaded, and then each time the Previous or Next buttons are clicked. This is done in a subroutine called GetRecord():
[C#]
private void GetRecord()
{
DataRow Row = dsAddress.Tables[0].Rows[CurrentRow];
lblCompany.Text = Row["company"].ToString();
lblRecipient.Text = Row["recipient"].ToString();
lblStreet.Text = Row["street"].ToString();
lblCity.Text = Row["city"].ToString();
lblCountry.Text = Row["country"].ToString();
lblSalutation.Text = Row["salutation"].ToString();
SetButtonState();
}
[Visual Basic]
Private Sub GetRecord()
With dsAddress.Tables(0).Rows(CurrentRow)
lblCompany.Text = .Item("company")
lblRecipient.Text = .Item("recipient")
lblStreet.Text = .Item("street")
lblCity.Text = .Item("city")
lblCountry.Text = .Item("country")
lblSalutation.Text = .Item("salutation")
End With
SetButtonState()
End Sub
[Delphi]
procedure Form2.GetRecord();
begin
with dsAddress.Tables[0].Rows[iCurrentRow] do begin
lblCompany.Text := Item['company'].ToString;
lblRecipient.Text := Item['recipient'].ToString;
lblStreet.Text := Item['street'].ToString;
lblCity.Text := Item['city'].ToString;
lblCountry.Text := Item['country'].ToString;
lblSalutation.Text := Item['salutation'].ToString;
end;
SetButtonState;
end;
When the Merge button is clicked, data is copied to the document's text fields. The text fields have been named after their corresponding database fields. This way, fields can be filled with data using a simple For Each loop on the TextControl.TextFields collection.
[C#]
private void cmdMerge_Click(object sender, System.EventArgs e)
{
foreach (TXTextControl.TextField Field in tx.TextFields)
{
Field.Text = dsAddress.Tables[0].Rows[CurrentRow][Field.Name].ToString();
}
}
[Visual Basic]
Private Sub cmdMerge_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdMerge.Click
Dim Field As TXTextControl.TextField
For Each Field In tx.TextFields
Field.Text = dsAddress.Tables(0).Rows(iCurrentRow).Item(Field.Name)
Next
End Sub
[Delphi]
procedure Form2.cmdMerge_Click(sender: System.Object; e: System.EventArgs);
var
Field : TXTextControl.TextField;
begin
for Field in tx.TextFields do begin
Field.Text := dsAddress.Tables[0].Rows[iCurrentRow].Item[Field.Name].ToString;
end;
end;