SQL Reporting Services Tips  

Main | About us | Contact us  

 

8) Handling Divide by Zero Errors

Bring up the Report Properties (by right clicking outside the report in Layout view)

Now go to the Code tab and write (or copy and paste) in this code:

Function CalculateFraction(ByVal Numerator As Double, ByVal Denominator As Double) As Double

 

If Denominator = 0 Then

          CalculateFraction = 0

Else

          CalculateFraction = Numerator / Denominator

End If

Return CalculateFraction

End Function

 

 

Now insert the following expression into the desired textbox and format according:

 

=Code.CalculateFraction(Fields!Numerator.Value, Fields!Denominator.Value)

 

You could also use the following IIF expression:

 

=IIF(Fields!Denominator.Value = 0, 0, Fields!Numerator.Value / IIF(Fields!Denominator.value = 0, 1, Fields!Denominator.Value))

 

But the best thing about using the Embedded Code it's easier to use and reusable.

 

© Copyright 2009. Reporting Services Tips.