How do I handle null nested objects in RDLC report that is bound to custom assembly object datasource?

前端 未结 3 382
孤街浪徒
孤街浪徒 2021-01-26 22:57

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

相关标签:
3条回答
  • 2021-01-26 23:06
    =iif(First(Fields!model.Value, "model") Is Nothing, "value is NULL", "value is not NULL")
    
    0 讨论(0)
  • 2021-01-26 23:13

    I don't know for sure how to fix your problem, but I have a couple of suggestions...

    1. Perhaps if you changed your IIF statement to be:

      IIf(IsNothing(Fields!TheNestedObject,"n/a", Fields!TheNestedObject.Value.Name))
      

      IIf always evaluates all of the arguments, so it is trying to evaluate TheNestedObject.Value. If TheNestedObject is NULL or NOTHING, then I wouldn't be surprised to see it throw an error.

    2. Another idea would be to modify your constructor to add an "empty" "B" object whenever there is no "B." For example, A.TheNestedObject would point to a "B" object which has no data. B.Id would be 0 (by default) unless you made it a nullable Int. B.Name would be "". Etc.

    0 讨论(0)
  • 2021-01-26 23:21

    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.

    Public Function GetName(ByRef obj As Object) As String
        If obj Is Nothing Then
            Return "n/a"
        Else : Return obj.Name
        End If
    End Function
    

    And then your textbox expression is:

    =Code.GetName(Fields!TheNestedObject.Value)
    

    The function returns "n/a" when it's null and the Name property when it's not.

    0 讨论(0)
提交回复
热议问题