Julie Camille Esteban February 2016

system.formatexception input string was not in a correct format when selecting a value in dropdownlist

Everytime I pick any value from the meatyqty dropdownlist even in the other dropdownlist, the error "input string was not in correct format. Here is my code:

protected void Page_Load(object sender, EventArgs e)
{
    int x;
    for (x = 0; x <= 100; x++)
    {
        ListItem item = new ListItem(x.ToString(),x.ToString());
        tidalqty.Items.Add(item);
        meatyqty.Items.Add(item);
        darknessqty.Items.Add(item);
        macaroniqty.Items.Add(item);
        cheesyqty.Items.Add(item);
        baconqty.Items.Add(item);
        loveqty.Items.Add(item);
    }        
}

protected void tidalqty_SelectedIndexChanged(object sender, EventArgs e)
{
    tidalprice.Text = ((double.Parse(tidalqty.SelectedValue)) * 250).ToString();
    computetotal();
}

protected void meatyqty_SelectedIndexChanged(object sender, EventArgs e)
{
    meatyprice.Text = ((double.Parse(meatyqty.SelectedValue)) * 500).ToString("f2");
    computetotal();
} 

void computetotal()
{
    double sub1 = double.Parse(tidalprice.Text);
    double sub2 = double.Parse(meatyprice.Text);

    subprice.Text = (sub1+sub3).ToString("f2");              
}

Answers


NikolaiDante February 2016

You're most likely running into trouble as you are adding to / rebuilding the list on each post back, so it's having trouble with SelectedValue.

Try:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
      int x;
      for (x = 0; x <= 100; x++)
      {
        ListItem item = new ListItem(x.ToString(),x.ToString());
        tidalqty.Items.Add(item);
        meatyqty.Items.Add(item);
        darknessqty.Items.Add(item);
        macaroniqty.Items.Add(item);
        cheesyqty.Items.Add(item);
        baconqty.Items.Add(item);
        loveqty.Items.Add(item);
      }       
    } 
}

I've basically added a call to Page.IsPostBack.

Post Status

Asked in February 2016
Viewed 3,922 times
Voted 7
Answered 1 times

Search




Leave an answer