NullReference Exception on simple two table MailMerge

I have a simple Word template with two tablestart/end sets (not nested) for two different tables that are included in a dataset along with a few other tables. I am doing a MailMerge.ExecuteWithRegions and sending the full dataset through, but I get a NullReference exception. If I take out one of the tablestart/end's and try it again it runs fine and fills the template as expected. I'm sure that I'm just doing something stupid here, but there error message is coming out of Aspose and it is not giving me any additional detail to figure out the problem so I am lost.

Is there someone that I can send the dataset and template to, to have a look? My code follows:

Document doc = new Document(templateTextBox.Text);

DataSet dataSet = new DataSet();

dataSet.ReadXml(dataTextBox.Text);

doc.MailMerge.ExecuteWithRegions(dataSet);

Please attach the document template and the dataset here using 'Options' tab of the post editor. Please note that all attachments on the forums are private will be available only to our team members.

Sorry, I did not see the option to attach files previously. They should be attached now. Here is the full error message:

System.NullReferenceException: Object reference not set to an instance of an object.
at ᢾ.ᢽ.ᣈ()
at ᢾ.ᢽ.Ջ(MailMerge ᣅ, IMailMergeDataSource ᣆ)
at Aspose.Words.Reporting.MailMerge.ExecuteWithRegions(DataSet dataSet)
at Lrs.Esf.DocumentRenderer.AsposeRenderer.Render(NotificationTemplate template, DataSet dataSet, EsfMessage parentMessage)

Hi,

The cause of the problem is that the second pair of region markings is placed at the run level. A paragraph break inserted before TableStart:EventParameters helps, see attached. However, to be honest, I have not heard about this restriction before. I'm going to consult with Roman in order to ascertain if this is a bug or an unsupported feature. Meanwhile I have logged this issue as #1318.

I’m not sure what you mean by “at the run level”, could you please explain? I see that you inserted a carriage return, but I’m not sure that is going to be possible in some circumstances. Please let me know the result of your conversation with Roman. Thanks.

Sorry for being unclear. I meant that in your template the TableStart and TableEnd fields are inserted in the middle of paragraph and it looks like an uncommon case. However, I can't see any restrictions in the code that would dictate that TableStart should be preceded with a paragraph break. So I'll surely post our investigations here in few hours.

Confirmed - this is by design. Mail merge regions must stay at the same level and they should not overlap. Obviously you have TableEnd from the first region and TableStart from the next region in the same paragraph which is treated wrong by the engine. We will fix the exception but the behaviour cannot be fixed as for now.

If placing regions in separate paragraph does not suit you, consider the use of DocumentBuilder or even manual insertion of nodes where needed. This is the only workaround available.

I agree with Dmitry.

Here are more tips:

  • Mail merge regions are mainly intended for repeatable blocks of text that need to be repeated for every row of your data source. In your case it is just a letter and you don't intent to repeat its header or part of its body several times, therefore you should not really be using mail merge regions. You have data in two tables and it needs to be merged into a single letter. A good option could be to create a query that returns all columns you need in a single table and merge with a single table.
  • Mail merge regions minimum granularity is a paragraph. That is a repeatable region will be at least a complete paragraph. Also, mail merge regions cannot overlap. An implication of these rules is that two mail merge regions cannot be within the same paragraph (that's what you have). We will fix the exception thrown, but the behaviour will stay as is.

Fixed in Aspose.Words 4.0.2. Now proper exception is thrown.

I appreciate your fixing the exception.

Unfortunately document builder is not a viable solution when you are trying to automate the filling of template documents. We were trying to use regions to solve the issue of doing dataset merging for both repeatable regions and single-value merges; both could exist in the same document. I guess we will have to merge the single-row tables into one single row table in the dataset to allow this to happen. That is not very logical from the template designers perspective though. The feature would still be nice to have if technical issues would allow it. What I am attempting to ask is that you do not throw it out on principle alone, as I feel the principle you have mentioned is not taking into account all of the viewpoints/added functionality.

Thanks,

Mark

Thank you for your suggestions. You are absolutely right, the current MailMerge engine is not very convenient when handling more complex merge scenarios. We are actually planning to rework our MailMerge engine in one of the next versions as lots of users request this. Your considerations will most probably be taken into account then.

Best regards,