ReportViewer Division by zero

為{幸葍}努か 提交于 2019-12-01 10:40:38

问题


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.Value, 0)

This normally works fine, but when both F1 and F2 are zero, I get "#Error" in the report, and I get this warning: "The Value expression for the textbox ‘textbox196’ contains an error: Attempted to divide by zero."

Why is that?


回答1:


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)



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/158508/reportviewer-division-by-zero

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!