ReportViewer Division by zero

前端 未结 3 1586
醉话见心
醉话见心 2021-01-16 13:21

I have some formulas in my reports, and to prevent divsion by zero I do like this in the expression field:

=IIF(Fields!F1.Value <> 0, Fields!F2.Value/Fields!F1.Va

相关标签:
3条回答
  • 2021-01-16 13:25

    There has to be a prettier way than this, but this should work:

    =IIF(Fields!F1.Value <> 0, Fields!F2.Value / 
       IIF(Fields!F1.Value <> 0, Fields!F1.Value, 42), 0)
    
    0 讨论(0)
  • 2021-01-16 13:26

    IIF() is just a function, and like with any function all the arguments are evaluated before the function is called, including Fields!F2.Value/Fields!F1.Value. In other words, it will attempt to divide by zero in spite of the Fields!F1.Value <> 0 condition.

    0 讨论(0)
  • 2021-01-16 13:43

    However, you can use

    if Fields!F1.Value <> 0 
    then 
    Fields!F2.Value/Fields!F1.Value
    else 0
    

    which should work, since it doesn't evaluate the then clause if the "if" section is false.

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