DataColumn.Expression error handling

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I need to compute a percentage in a DataTable, so i'm trying to use the Expression property provided by the DataColumn object. My problem is the classic division by zero error.
This is my expression:
"IIF(budget_ext_price = 0, 'NaN', material_margin / budget_ext_price)"
Now, it seems that the material_margin / budget_ext_price expression is being evaluated regardless of the condition. If i replace the '/' with '-', I get correctly NaN in one row, though with '/' I get division by zero exception. Is the false part really being evaluated, even in the case the condition is true?
 
Hi pali,

This is vb behaviour where IIF is a procedure that you pass parameters to.
It is not the same as ?: operator in C# I believe.
Thus you might modify it a bit just to avoid an exception:
IIF(budget_ext_price = 0, 'NaN', material_margin / iif(budget_ext_price=0,
1, budget_ext_price))"

Didn't test it but it might work :-)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

pali said:
Hi,
I need to compute a percentage in a DataTable, so i'm trying to use the
Expression property provided by the DataColumn object. My problem is the
classic division by zero error.
This is my expression:
"IIF(budget_ext_price = 0, 'NaN', material_margin / budget_ext_price)"
Now, it seems that the material_margin / budget_ext_price expression is
being evaluated regardless of the condition. If i replace the '/' with '-',
I get correctly NaN in one row, though with '/' I get division by zero
exception. Is the false part really being evaluated, even in the case the
condition is true?
 
Hi, thanx for the reply, this doesn't solve my problem though.
In your example, I would never get the NaN message, since budget_ext_price would never be 0 but 1 instead, and I would really like to display the NaN message... I solved it on the database side in the mean time (because of the tight schedule), but I was wondering, is there ANY way to get the behavior I want from the Expression (i. e. in case of DIV/0 display message, otherwise the result)

Miha Markic said:
Hi pali,

This is vb behaviour where IIF is a procedure that you pass parameters to.
It is not the same as ?: operator in C# I believe.
Thus you might modify it a bit just to avoid an exception:
IIF(budget_ext_price = 0, 'NaN', material_margin / iif(budget_ext_price=0,
1, budget_ext_price))"

Didn't test it but it might work :-)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

pali said:
Hi,
I need to compute a percentage in a DataTable, so i'm trying to use the
Expression property provided by the DataColumn object. My problem is the
classic division by zero error.
This is my expression:
"IIF(budget_ext_price = 0, 'NaN', material_margin / budget_ext_price)"
Now, it seems that the material_margin / budget_ext_price expression is
being evaluated regardless of the condition. If i replace the '/' with '-',
I get correctly NaN in one row, though with '/' I get division by zero
exception. Is the false part really being evaluated, even in the case the
condition is true?
 
The following exprsesion should give you 0 if Column2 is 0, and the result
of the division otherwise.
You could wrap it in another IIF, that would print out 'NaN' if this whole
thing is 0, and the thing itself otherwise.

(Column1 / IIF(Column2 = 0, 1, Column2) ) * IIF (Column2 = 0, 0, 1)

pali said:
Hi,
I need to compute a percentage in a DataTable, so i'm trying to use the
Expression property provided by the DataColumn object. My problem is the
classic division by zero error.
This is my expression:
"IIF(budget_ext_price = 0, 'NaN', material_margin / budget_ext_price)"
Now, it seems that the material_margin / budget_ext_price expression is
being evaluated regardless of the condition. If i replace the '/' with '-',
I get correctly NaN in one row, though with '/' I get division by zero
exception. Is the false part really being evaluated, even in the case the
condition is true?
 
pali said:
Hi, thanx for the reply, this doesn't solve my problem though.
In your example, I would never get the NaN message, since budget_ext_price
would never be 0 but 1 instead, and I would really like to display the NaN
message...

Why not? I changed only the result which you never actually get...

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

I solved it on the database side in the mean time (because of the tight
schedule), but I was wondering, is there ANY way to get the behavior I want
from the Expression (i. e. in case of DIV/0 display message, otherwise the
result)
Miha Markic said:
Hi pali,

This is vb behaviour where IIF is a procedure that you pass parameters to.
It is not the same as ?: operator in C# I believe.
Thus you might modify it a bit just to avoid an exception:
IIF(budget_ext_price = 0, 'NaN', material_margin / iif(budget_ext_price=0,
1, budget_ext_price))"

Didn't test it but it might work :-)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

pali said:
Hi,
I need to compute a percentage in a DataTable, so i'm trying to use
the
Expression property provided by the DataColumn object. My problem is the
classic division by zero error.
This is my expression:
"IIF(budget_ext_price = 0, 'NaN', material_margin / budget_ext_price)"
Now, it seems that the material_margin / budget_ext_price expression
is
being evaluated regardless of the condition. If i replace the '/' with '-',
I get correctly NaN in one row, though with '/' I get division by zero
exception. Is the false part really being evaluated, even in the case the
condition is true?
 
Back
Top