MS Reporting Services

Location: BlogsComments    
Posted by: Dale Thursday, July 26, 2007

Divide by zero: I had an odd experience today when I tried to prevent a divide by zero by using the IIF statement e.g. =IIf(Term2 <> 0, Term1/Term2, 0) however it appears to try and evaluate 'Term1/Term2' even if it 'Term2 = 0' very strange - so I had to use another IIf statement to ensure that a valid computation happens even though it will never be used e.g. =IIf(Term2 <> 0, Term1/(IIf(Term2 <> 0, Term2, 1), 0).

IIf revisited: OK - so now I understand a bit more about VB I've found out that the IIf statement always evaluates both expressions and then returns one of them. The previous *hack* didn't help when it came time to try and display a parameter which could be null (Nothing). The solution it seems was to write a small function which is embedded in the report. This is done on the Report/Parameters/Code tab. And there is no support for debugging your code so good luck!

Public Shared Function HandleNull(ByVal Param As Microsoft.ReportingServices.ReportProcessing.ReportObjectModel.Parameter) As String
    If Param.Value Is Nothing Then
      Return "All"
    Else
      Return Param.Value.ToString()
    End If
End Function


and then in my text box I call it as:

Code.HandleNull(Parameters!Rejected)

Update: The null problem can be solved without a function as follows:

=IIf(IsNothing(Parameters!Rejected.Value), "All", CStr(Parameters!Rejected.Value))

Permalink |  Trackback

Your name:
Title:
Comment:
Add Comment   Cancel