I have an RDLC report that I am rendering directly to the Response Stream as PDF (rather than using the ReportViewer). In the code that renders the report, it’s DataSource is bound to a List(Of ClassA) objects defined in a custom assembly. This seems to work for the most part. My problem is that I can’t seem to handle the situation where a nested object is null. For example, given ClassA and ClassB (the nested object) defined as follows:
Public Class ClassA
Public Id As Integer
Public Name As String
Public TheNestedObject As ClassB
End Class
Public Class ClassB
Public Id As Integer
Public Name As String
Public TheParentObject As ClassA
End Class
Whenever I try to conditionally display an “N/A” if Class B is null in my expression as follows:
=IIf(IsNothing(Fields!TheNestedObject.Value,"n/a", Fields!TheNestedObject.Value.Name))
the report displays “#Error” if TheNestedObject is null. If TheNestedObject is not null, it correctly displays the Name.
What am I doing wrong here?
Thanks!!!
The iif function evaluates all arguments and thus Fields!TheNestedObject.Value.Name gets evaluated and gives the error since Fields!TheNestedObject.Value is null.
I ended up adding some custom code to the report. It’s under report properties -> Code tab.
And then your textbox expression is:
The function returns “n/a” when it’s null and the Name property when it’s not.