Insert image from databse into MailMerge field in Word document

Hi
I’m trying to insert an image from a database into a word doc using mailmerge and I’m getting an error
Buffer cannot be null.
Parameter name: buffer

This is the code…

SqlCommand cmd = new SqlCommand("up_SelectAdvisorDetails", connStr);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
string pUserName = Convert.ToString(Session["UserName"]);
cmd.Parameters.AddWithValue("@UserName", pUserName);

connStr.Open();

SqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
    // Set up the event handler for image fields.
    doc.MailMerge.FieldMergingCallback = new HandleMergeImageFieldFromBlob();

    if (!dataReader["SignatureImage"].Equals(DBNull.Value))
    {
        doc.MailMerge.Execute(new string[]
            {
                "AdviserSignature"
            },
            new object[]
            {
                dataReader["SignatureImage"]
            });
    }

    if (!dataReader["LogoImage"].Equals(DBNull.Value))
    {
        doc.MailMerge.Execute(new string[]
            {
                "AdviserLogo"
            },
            new object[]
            {
                dataReader["LogoImage"]
            });
    }

}
dataReader.Close();

Here’ s the merge field in the document…

«Image:AdviserLogo»

I’ve downloaded the lastest version today. Can you help me?
thanks
Peter

Hello
Thanks for your request. Please see the following link to learn how to insert images from a Data Base during MailMerge:
https://docs.aspose.com/words/net/types-of-mail-merge-operations/
Hope this helps.
Best regards,

yes, I have read that article before posting my problem.

do you have any other examples

Hi
Thanks for your request. such exception can occur when you try to create a MemoryStream and pass null as parameter:

byte[] buff = null;
MemoryStream ms = new MemoryStream(buff);

So, in your implementation of IFieldMergingCallback, you should make sure buffer is not null.
Best regards,

wow, you right,
who would have thought that the error message
Buffer cannot be null.
Parameter name: buffer

would be raised if you pass null as parameter?
absolute genius.

Hi Peter,
It is perfect that you managed to resolve the problem. Please feel free to ask in case of any issues, we are always glad to help you.
Best regards,

Hi
I haven’t solved the problem.

I’m not a C# programmer, can you provide the code to do the check for a null buffer?

Why would the buffer be null if the sql is returning the images from the database?

Can you give me an example that doesn’t use a datatable?

thanks
Peter

Hi Peter,
Thanks for your request.

  1. I supposed you are using C# because the code example provided in your first post is written in C#. Anyways, here is simple code example that check for nulls in IFieldMergingCallback:
[Test]
public void Test001()
{
    // Open template.
    Document doc = new Document(@"Test001\in.doc");
    // Specify field merging callback.
    doc.MailMerge.FieldMergingCallback = new FieldMergingCallback();
    // Execute mail merge.
    doc.MailMerge.Execute(new string[]
    {
        "myimage",
        "nullimage"
    }, new object[]
    {
        File.ReadAllBytes(@"Test001\test.png"), null
    });
    // Save output.
    doc.Save(@"Test001\out.doc");
}
private class FieldMergingCallback: IFieldMergingCallback
{
    public void FieldMerging(FieldMergingArgs args)
    {
        // do nothing.
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
    {
        if (args.FieldValue != null)
        {
            byte[] imageBytes = (byte[]) args.FieldValue;
            args.ImageStream = new MemoryStream(imageBytes);
        }
    }
}
  1. Unfortunately, I cannot answer this question because i did not see your database. Maybe there are records without image bytes.
  2. See the example above.

Best regards,