Format issue in Merge field

Hi
i have following code for format mailmerge field
{ MERGEFIELD "Price" \# $#,### }

its working fine.My probelm is if i have a NULL value return from DB its display as $ Symbol alone.My reqiurement is it should display as $0 if the value is null

Hi Ajeesh,

Thanks for the additional information.

In that case, you can wrap your merge field in an IF field based off this condition. Please modify your template as below:


{ IF "{ MERGEFIELD "Price" \# $#,### }" = "$" "$0" "{ MERGEFIELD "Price" \# $#,### }" }


I hope this will help.


Hi Ajeesh,

Thanks for your inquiry. I think, you can achieve this by using any of the following approaches:

1) You can enclose your merge field inside an IF field in your template document as follows:

{ IF { MERGEFIELD "Price" \# $#,### } = "" "$0" "{ MERGEFIELD "Price" \# $#,### }" }

Moreover, I have attached a test document here for you to play with.

2) You can implement IFieldMergingCallback interface if you want to control how data is inserted into merge fields during a mail merge operation. Please try using the following code snippet:

Document doc = new Document(@"c:\test\Price.docx");

doc.MailMerge.FieldMergingCallback = new HandleMergeFields();
doc.MailMerge.Execute(
new string[] { "Price" },
new object[] { "" });

doc.Save(@"c:\test\out.docx");

private class HandleMergeFields : IFieldMergingCallback
{
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
{
if (e.FieldName == "Price")
{
DocumentBuilder builder = new DocumentBuilder(e.Document);
builder.MoveToField(e.Field, false);

if (string.IsNullOrEmpty(e.FieldValue.ToString()))
{
builder.Write("$0");
e.Text = string.Empty;
}
}
}

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
{ }
}

I hope, this will help.

Best Regards,