Mailmerge fails when some data is Null


#1

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?


#2

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?


#3

I don’t have a solution yet, but here is what I’ve found:

1. 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.

2. 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.

3. 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.

4. 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.


#4

DBnull occurs frequently in datasets in our code - in our database, <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

blank strings are always stored as null and when that data is then

retrieved into the dataset, we get DBNull as the value of the field.

The attatched rar file contains a VB.NET (2002) project with my test

code, the word document I am using as a source file and an XML file with

the data contained in the dataset once it has been filled from the

database (as returned by oDS.GetXml()).

I am reliably reproducing the problem with these, so if you figure out

what's going on, please do let me know.


#5

I load your data into a DataSet and it works fine. I guess you are using .NET 1.0 and the problem could be related to it.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

DataSet ds = new DataSet();

ds.ReadXml(TestUtil.BuildTestFileName(@"MailMerge\TestMailMergeDBNull.xml"));

Document doc = TestUtil.Open(@"MailMerge\TestMailMergeDBNull.doc");

doc.MailMerge.Execute(ds.Tables[0]);

TestUtil.Save(doc, @"MailMerge\TestMailMergeDBNull Out.doc");

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.


#6

I have both 1.0.3705 and 1.1.4322 of the .NET framework installed on my PC simultaneously. Call stack is

AsposeTest.exe!AsposeTest.FrmAspose.btnMerge_Click(Object sender = {System.Windows.Forms.Button}, System.EventArgs e = {System.EventArgs}) Line 159 Basic
system.windows.forms.dll!System.Windows.Forms.Control::OnClick(System.EventArgs e = {System.EventArgs}) + 0x62 bytes
system.windows.forms.dll!System.Windows.Forms.Button::OnClick(System.EventArgs e = {System.EventArgs}) + 0x38 bytes
system.windows.forms.dll!System.Windows.Forms.Button::OnMouseUp(System.Windows.Forms.MouseEventArgs mevent = {X=59 Y=9 Button=Left}) + 0x11c bytes
system.windows.forms.dll!System.Windows.Forms.Control::WmMouseUp(System.Windows.Forms.Message m = {System.Windows.Forms.Message}, System.Windows.Forms.MouseButtons button = Left, __int32 clicks = 1) + 0x235 bytes
system.windows.forms.dll!System.Windows.Forms.Control::WndProc(System.Windows.Forms.Message m = {System.Windows.Forms.Message}) + 0x472 bytes
system.windows.forms.dll!System.Windows.Forms.ButtonBase::WndProc(System.Windows.Forms.Message m = {System.Windows.Forms.Message}) + 0x11a bytes
system.windows.forms.dll!System.Windows.Forms.Button::WndProc(System.Windows.Forms.Message m = {System.Windows.Forms.Message}) + 0x8d bytes
system.windows.forms.dll!ControlNativeWindow::OnMessage(System.Windows.Forms.Message m = {System.Windows.Forms.Message}) + 0x19 bytes
system.windows.forms.dll!ControlNativeWindow::WndProc(System.Windows.Forms.Message m = {System.Windows.Forms.Message}) + 0xda bytes
system.windows.forms.dll!System.Windows.Forms.NativeWindow:: DebuggableCallback(__int32 hWnd = 133184, __int32 msg = 514, __int32 wparam = 0, __int32 lparam = 589883) + 0x39 bytes
system.windows.forms.dll!System.Windows.Forms.Application::ComponentManagerSystem.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(__int32 dwComponentID = 1, __int32 reason = -1, __int32 pvLoopData = 0) + 0x2c1 bytes
system.windows.forms.dll!ThreadContext::RunMessageLoop(__int32 reason = -1, System.Windows.Forms.ApplicationContext context = {System.Windows.Forms.ApplicationContext}) + 0x1c5 bytes
system.windows.forms.dll!System.Windows.Forms.Application::Run(System.Windows.Forms.Form mainForm = {AsposeTest.FrmAspose}) + 0x37 bytes
AsposeTest.exe!AsposeTest.FrmAspose.Main() Line 3 + 0x1d bytes Basic


#7

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.


#8

Don’t worry about the stack trace. I’ve reproduced the problem. It occurs only with typed data sets, will fix asap.


#9

Get the latest Aspose.Word 2.1.2 which handles DBNulls safely and also provides uniform handling of nulls and DBNulls, for more info see http://www.aspose.com/blogs/roman.korchagin/archive/2005/01/07/390.aspx


#10

That’s great. Just to confirm, I’ve now had time to test it here and all seems to be working perfectly. Thanks very much for your help.