divide by 0 error

M

Matt

Scenario:
A dataset exists with a number of columns in it. I want to add a new column
that will divide ColA by ColB.

How I've accomplished this is pretty simple, simply create an instance of
DataColumn, and set the .Expression property to ColA / ColB.

The problem is when I get to a row where ColB is 0, a divide by zero error
occurs. This seems like it should be something so common that it would be
heavily documented and such as to how to handle, but can't find anything.
 
K

Kevin Yu [MSFT]

Hi Matt,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you have an Expression Column in a
DataTable whose value is a division from other 2 columns. When a column
value is 0, a Devide by 0 exception is thrown. If there is any
misunderstanding, please feel free to let me know.

Are you binding the whole DataSet to a DataGridView? If so, you can handle
the DataGridView.DataError to workaround this error dialogbox. Here is an
example.

private void dataGridView1_DataError(object sender,
DataGridViewDataErrorEventArgs e)
{
if (e.Exception is System.Data.EvaluateException)
//Do whatever you need.
}

If anything is unclear, please feel free to reply to the post.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
B

Brendan Green

..Expression = "IFF(ColB = 0, 0, ColA/ColB"

Will set the column to 0 if ColB is zero, otherwise does the division.
 
M

msnews.microsoft.com

You can if the expression to only run when it will not end up in divide by
zero.

You can calculate the aggregate on the data side with a conditional SQL
query.

If Binding
----------
You can set up the error event on the object you are binding and have it set
a specific value.

You can set up the the binding event to evaluate and determine what to put
into the grid.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think Outside the Box!
*************************************************
 
M

Matt

that took care of the problem... still have a grid that I need to look at,
but other suggestions will probably help there.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top