Your question, your audience. Choose who sees your identity—and your question—with question security.

Hi,

I would like to get help in adding a check to the formula down below that takes care of the divide by zero error, if possible.

Thank you

I would like to get help in adding a check to the formula down below that takes care of the divide by zero error, if possible.

```
IIf(CDec(Sum(IIf(Fields!OrderLineStatus.Value = "OPEN", Fields!OrderBalanceQty.Value, CDec(0)))) <> 0, (CDec(Sum(IIf(Fields!OrderLineStatus.Value = "OPEN", (Fields!OrderBalanceQty.Value * Fields!OrderPrice.Value), CDec(0)))) / CDec(Sum(IIf(Fields!OrderLineStatus.Value = "OPEN", Fields!OrderBalanceQty.Value, CDec(0))))), 0)
```

Thank you

Alternatively, you could use the little known IFERROR() function to replace your error with a suitable value:

```
=IFERROR(Your Expression Here, 0)
```

And it would give you a zero value if the expression causes a calculation error (in this case, divide by zero).

My mistake - glad your solution did the trick!

So I decided to give the following a try: IsError(1/0)

And that returned False. Erm, did I miss something? Apparently I did.

Then I put the following in a textbox: =1/0

Guess what? That actually runs without any errors! And it will produce the string "Infinity".

Alright, what about the following: =IsError(1.1/"duh")

That will produce #Error on the report and the following rsRuntimeErrorInExpression

To be honest, so far I haven't succeeded in getting a True out of the IsError function. So I decided to not use it.

Another thing which I've discovered is that the expression result

Let's move to the next step of the investigation. If 1/0 doesn't generate a "div by zero" error, doesn't that mean that the following should work?

=IIF(Fields!Number2.Value = 0, "divzero", Fields!Number1.Value / Fields!Number2.Value)

Well, apparently it does work! But not in all situations. And that's where my confusion comes from. There's actually a hidden gem in the following Brian Welcker blog post: End of Amnesia (Avoiding Divide By Zero Errors)

If you read his article you'd think that it confirms what I explained earlier. And it does. But there's a hidden condition in the second paragraph:

"This works fine unless a value in the Cost field is zero (

In other words, the simple IIF(B = 0, "it's zero", A/B) will work fine as long as the data type of A and B is not decimal. But if they are decimal, you'll start seeing #Error in your report and the "Attempted to divide by zero" rsRuntimeErrorInExpression

Why this behavior difference occurs isn't yet 100% clear to me but it must be either related to how VB has implemented the / operator for the different data types OR to how the report processing engine handles different exceptions. There must be some code somewhere that decides to display "Infinity" instead of generating an error.

I hope you found reading this as interesting as much as I learned from the research.

Have fun!

Valentino.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

What seems to be forgotten frequently is that SSRS evaluates the whole expression. It doesn't stop evaluating the ELSE part of the IIF, not even if the THEN part evaluates to True. So adding an IIF around the existing expression will still result in divide by zero, as Nicobo wants to point out with that link.

With that in mind, you need to prevent that the denominator is zero under any circumstances. The most simple way to achieve that is by putting the condition in the denominator itself:

Open in new window

But that's only part of the solution because instead of "division by zero" this will now result in Number1.So we need another IIF around the whole thing, to be able to output another value in case of div by zero:

Open in new window