divid by zero exception Error

Horse06Horse06 Member Posts: 496
edited 2013-12-30 in NAV Three Tier
How expert, I have a dataset X in the RDLC report, which has expression as (sum A/sum B)*100.
But when A is equal or less than zero, X will be showing ERROR since it is divided by zero exception. How to add the if condition to the dataset X to avoid the error? Thank you!

Comments

  • geordiegeordie Member Posts: 655
    If you wanna perform your control directly in TextBox you can use an expression like this:
    =IIf(Sum(Fields!B.Value)=0,0,Sum(Fields!A.Value)/Sum(Fields!B.Value)*100)
    
  • Horse06Horse06 Member Posts: 496
    Thank you expert! I tried it as below, but it still shows error. Thank you!

    =IIF((Sum(Fields!TL1.Value)-Sum(Fields!TL2.Value))=0,0,sum(fields!Mship1.Value)-sum(Fields!Mship2.Value))/(Sum(Fields!TL1.Value)-Sum(Fields!TL2.Value))*100)
  • Horse06Horse06 Member Posts: 496
    Hi Expert,
    I tried again using the following expression for the textbox control
    =IIf(Sum(Fields!TS.Value)=0,0,Sum(Fields!MS.Value)/Sum(Fields!TS.Value)*100)
    It is the same result. For those TS that are not zero, the numbers are correct, but if the TS is zero, it stil shows error.
    Please advise, expert! Thank you!
  • geordiegeordie Member Posts: 655
    Just checked...didn't know IIf statement was so tricky: preparing the argument list the compiler calls every function in every expression so it's not possible to rely on "else" part not being executed. To avoid this it's possible to use a nested IIf, transforming previous expression in:
    =IIf(Sum(Fields!TS.Value)=0,0,Sum(Fields!MS.Value)/IIf(Sum(Fields!TS.Value)=0,1,Sum(Fields!TS.Value))*100)
    

    Try this and let me know :D
  • william_marcelinuswilliam_marcelinus Member Posts: 34
    Dear,

    Make Sure checking both value in divide calculation, try this and hope it works :)

    =IIF(SUM(Fields!TS.Value) = 0,0,IIF(SUM(Fields!MS.Value)=0,0,(SUM(Fields!MS.Value)/SUM(Fields!TS.Value))*100)))
  • Horse06Horse06 Member Posts: 496
    Thank you, experts!
    I tested both. geordie's code is working now. Thank you very much!
    william_marcelinus's code seems like the error will not go away.
    Thank you for both experts!
Sign In or Register to comment.