DivideByZeroException  
Author Message
BrettDerry





PostPosted: Visual C# Language, DivideByZeroException Top

Hi All,

This is my first post here as a wet-behind-the-ears C# (primarily web) programmer, so please go easy on me and don't bang your head up the wall when/if I ask a dumb question!!!

I've been given a fairly simple currency conversion form to create which will be hosted on our SharePoint Intranet. It basically works out what the margin is on each foreign exchange transaction that our travel stores make.

In VS, the code all renders/parses fine and I get no errors until I try and make a calculation. The error message is: 'Attempted to divide by zero.' I understand what the error means but I've got three lines of code that get values from text boxes on my form and convert them to decimals, which then go on to form the basis of my calculations.

Here is my code.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

// declare (default) values for margin and user inputs
decimal dMinMargin = 3.00M;
decimal dSaleAmount = 0.00M;
decimal dIceRate = 0.00M;
decimal dSellingRate = 0.00M;

// declare values for margin acheived and the maximum selling rate
decimal dMarginAchieved = 0.00M;
decimal dMaxSellRate = 0.00M;

// declare values for profit and margin
decimal dOverallProfit = 0.00M;
decimal dOverallMargin = 0.00M;

//declare values for overall cost of currency and overall sale proceeds
decimal dCost = 0.00M;
decimal dSaleProceeds = 0.00M;
decimal dProfit = 0.00M;

protected void btnCalculate_Click(object sender, EventArgs e)
{
// get values declared from text boxes/user input
dSaleAmount = (Convert.ToDecimal(Request.ServerVariables.Get("txtSaleAmount")));
dIceRate = (Convert.ToDecimal(Request.ServerVariables.Get("txtLastIceRate")));
dSellingRate = (Convert.ToDecimal(Request.ServerVariables.Get("txtSellingRate")));

// populate empty margin and max sell rate labels

dMarginAchieved = (dIceRate - dSellingRate) / dIceRate;
dMaxSellRate = dIceRate * (1 - dMinMargin);

lblMarginAchieved.Text = dMarginAchieved.ToString();
lblMaxSellRate.Text = dMaxSellRate.ToString();

// work out cost of currency and sale proceeds to then work out overall profit to be made by shops
dCost = dSaleAmount / dIceRate;
dSaleProceeds = dSaleAmount / dSellingRate;

dProfit = dSaleProceeds - dCost;
dOverallMargin = dOverallProfit / dProfit;

lblOverallProfit.Text = dOverallProfit.ToString();
lblOverallMargin.Text = lblMarginAchieved.Text;

}
}

Does anyone know why the values aren't being passed from the form

Hope someone can help!

Thanks in advance,
Brett



Visual C#10  
 
 
Paul Louth





PostPosted: Visual C# Language, DivideByZeroException Top

I assume you're expecting these to return the values from the textboxes

dSaleAmount = (Convert.ToDecimal(Request.ServerVariables.Get("txtSaleAmount")));
dIceRate = (Convert.ToDecimal(Request.ServerVariables.Get("txtLastIceRate")));
dSellingRate = (Convert.ToDecimal(Request.ServerVariables.Get("txtSellingRate")));

If so, that's not going to work. I assume you have ASP.NET text-boxes on your form If so, just do this:

saleAmount = Convert.ToDecimal(saleAmountTextBox.Text);

Obviously you should do validation on the text too. But I assume you just want to get this working.


 
 
BrettDerry





PostPosted: Visual C# Language, DivideByZeroException Top

Paul,

Many thanks for that, it works fine.

From what I've seen/used/programmed over the last week (and I've only been doing C# for one week!) it seems that a lot of the things I've been trying to do I've been doing in an over-complicated manner; particularly from a front-end, Dreamweaver and CSS boy like myself!

Thanks again,
Brett

 
 
Paul Louth





PostPosted: Visual C# Language, DivideByZeroException Top

Yep ASP.NET does try to hide a lot of painful stuff of dealing with form data from you.  I'd definitely recommend picking up a book on ASP.NET though, as there are a few gotchas that you'll probably hit at some point.  Either that, or just ask here :) 

 


 
 
Gabriel Lozano-Moran





PostPosted: Visual C# Language, DivideByZeroException Top

Also consider using the Decimal.TryParse method to convert that string to a decimal.