I see you get a NullReferenceException if a DataTable has a foreign key with some NULL values.
I created a simple example with a TextBlock class/table with a BackColor property which refers to a CustomColor class/table: the color is nullable for “transparent” color.
Case 1) (I moved this case to post 203684, see below)
No check for nulls are done in the template: you get a NullReferenceException for both custom classes and DataTable examples
==> This is right, but it would be nice if you could give some context to the exception, because with a long template it is very difficult to find where is missing the check for “null”!
With a check for “null” (either with “?.” or an explicit “if”) the example with custom classes works correctly, but the example with DataTables continues to give a NullReferenceException.
This is the syntax I tried and with custom classes it works correctly, but with DataTables an exception is thrown when Color is NULL in TextBlock.
For custom classes (works):
<<foreach [textBlock in textBlocks]>> <<[BackColor?.Name]>> <</foreach>>
For DataTables (does not work):
<<foreach [textBlock in TextBlocks]>> <<[CustomColors?.Name]>> <</foreach>>
Then I tried to check with “.Any()” on DataTables and it worked, but this is not intuitive and seems a bug to me: why you get an empty list of DataRows (or an empty DataRow, I don’t know) instead of “null”? This is not a list, it is a reference to a single item which does not exist…
<<foreach [textBlock in TextBlocks]>> <<if [CustomColors.Any()]>> <<[CustomColors.Name]>> <</if>><</foreach>>
Please fix this returning “null” in this case, so that it is possible to use the same simple syntax “?.” that you can use with custom classes, because having to add an “if” with “Any()” on every field makes the template much more complex.
Attached you can find all the described examples:
Aspose.Words - NullableForeignKey.zip (58.8 KB)
Note: in the example, to try with/without “null” values and see/hide the errors, uncomment/comment the first line in Common.cs to define ADD_NULL_VALUES.