format exception was unhandled by user code in c#

2.4k views Asked by At

The error message keeps popping up. Also, when I running the codes, it seems it's not working at all. I'm trying passing data to another page. Any suggestion? This is the main page code,

public partial class _Default : System.Web.UI.Page
{
static int numBeachBookingInt = 0;
static int numBushBookingInt = 0;
static decimal totRevenue = 0;

protected void Page_Load(object sender, EventArgs e)
{
    string totalRevenue;
    if (Convert.ToString(Session["confirmBooking"]) == "confirm" && Convert.ToString(Session["bachType"]) == "bush")
    {
        totalRevenue = (string)(Session["totalRevenue"]);
        totRevenue += decimal.Parse(totalRevenue);
        totRevenueLabel.Text = String.Format("{0:c}", totRevenue);
        numBushBooking += 1;
        numBushHouseLabel.Text = numBushBooking.ToString();
        Session["confirmBooking"] = "no current booking";
        Session["bachType"] = "none";
    }
    else if (Convert.ToString(Session["confirmBooking"]) == "confirm" && Convert.ToString(Session["bachType"]) == "beach")
    {
        totalRevenue = (string)(Session["totalRevenue"]);
        totRevenue += decimal.Parse(totalRevenue);
        totRevenueLabel.Text = String.Format("{0:c}", totRevenue);
        numBeachBooking += 1;
        numBeachHouseLabel.Text = numBeachBooking.ToString();
        Session["confirmBooking"] = "no current booking";
        Session["bachType"] = "none";
    }
    numBeachHouseLabel.Text = numBeachBooking.ToString();
    numBushHouseLabel.Text = numBushBooking.ToString();

this is the second.

protected void confirmButton_Click(object sender, EventArgs e)
{
    Session["confirmBooking"] = "confirm";
    Session["totalRevenue"] = totalRateLabel.Text;
    switch (bachTypeRadioButtonList.Text)
    {
        case "Beach":
            Session["bachType"] = "beach";
            break;
        case "Bush":
            Session["bachType"] = "bush";
            break;
        default:
            Session["bachType"] = "none";
            break;
    }
    Response.Redirect("MainBookingForm.aspx");
}
1

There are 1 answers

4
musical_coder On BEST ANSWER

It's hard to know what you're exact problem is without a stack trace. However, based on Googling "Format exception was unhandeled by user code", it's likely that one of the decimal.Parse(totalRevenue); statements is the problem. Since the string value of totalRevenue is set from totalRateLabel.Text, I wouldn't be surprised if that label has special characters (like a dollar sign) that you need to remove first.