I am currently evaluating Aspose to see if we can replace our current mail merge automation code using the component. Nearly everything seems to work, with only one problem.
I am performing a mailmerge against a data table in a dataset. If any of the columns in the dataset contain nulls (as they will for some of our data) then the mailmerge fails with an Invalid Cast error stating that it is not permitted to cast from DBNull to other types.
Is there a workaround for this problem or a setting I need to change to avoid it?
Thanks for the report. In our tests nulls are correctly handled, please post a fragment of your code here. However, its good that you mentioned DBNull, I will check for this as I don’t think we handle it.
Just out of interest, let me know whether do you expect the fields in the document to be left intact or removed when the value in the database is null?
I don’t have a solution yet, but here is what I’ve found:
DBNull is somewhat rare in .NET. NULL value is valid for a database field, while DBNull means unknown\uninitialized value. I was able to get DBNull only for newly created uninitalized rows. Make sure DBNulls in your case are legitimate and not due to an error in your application.
Aspose.Word handles nulls in data source okay. If it encounters null, it does not change the value of the merge field and leaves it intact.
Aspose.Word handles DBNull in data source okay too. If DBNull is encountered, it is converted to empty string and the merge field in the document is replaced with the empty string.
Obviously the difference in handling of null and DBNull by Aspose.Word during mail merge is not nice. Aspose.Word was designed to handle nulls as described above and was not designed to handle DBNulls so it just happened to rely on DBNull.ToString() that returns an empty string. It might be nice to handle nulls and DBNulls in the same way and also allow an option for the fields to either stay intact or be replaced with empty strings. We might do something about this later on.
Basically I cannot reproduce the problem you have. Please send some code and data to me to word@aspose.com if you want me to look at the problem further.
Can you please send me the stack trace that comes with the exception, it will help me to pinpoint where the exception is thrown and I will try to handle DBNulls safely there.
Thanks, but this is not really the stack trace I’m after.
I need a stack trace that shows which functions inside Aspose.Word were called when the exception occurred. Of course you will see only their obfuscated names, like “a”, “bb” and so on, but I will be able to decode them back here.
I think your stack trace is generated by the exception handler in Application.Run method that runs the message loop.
If you can still get the proper stack trace somehow it would be nice. I’m looking at articles about DBNulls, something is really strange happening at your side.