0

I have been Written A Simple Expression To Display 0 When I divide by 0 As Exception:

I Use Visual Studio 2008 And Visual Studio 2019

=IIF ( Sum(Fields!STUDENTCOUNT.Value) = 0, 0 , Sum(Fields!CONTINUINGSTUDENTCOUNT.Value) / Sum(Fields!STUDENTCOUNT.Value) )

Samar
  • 13
  • 5
  • Possible duplicate of [How to handle IIF or Switch divide by zero giving #ERROR?](https://stackoverflow.com/questions/15750896/how-to-handle-iif-or-switch-divide-by-zero-giving-error) – Chris Latta Jul 26 '19 at 00:03

1 Answers1

1

You didn't explain the issue very well but I can see what the issue is, just from the expression. With IIF statements, the entire statement is evaluated as soon as the cell is reached in the execution. This means that no matter how you try to avoid the divide by zero error in this expression, it will still evaluate the false condition and generate the error. I'm going to borrow some custom code from this answer to give you the solution.

Public Function Divide (ByVal Dividend As Double, ByVal Divisor As Double)
  If IsNothing(Divisor) Or Divisor = 0
    Return 0
  Else
    Return Dividend/Divisor
  End If
End Function

To add this to your code, right click outside of the report in the designer window and go to Report Properties. Click the Code tab and enter the previous code into the editor. Then, to call the code, you'll use the following expression.

=Code.Divide(Sum(Fields!CONTINUINGSTUDENTCOUNT.Value), Sum(Fields!STUDENTCOUNT.Value))
Steve-o169
  • 2,066
  • 1
  • 12
  • 21