0

I am trying to calculate a value when a item of a drop down list is selected. I am trying to do this using a switch but it doesn't work. It always goes to the default value and it doesn't see a value of a variable.

I'm building my drop down list dynamic from my database.

This is the code I have so far:

protected void ddlClub_SelectedIndexChanged(object sender, EventArgs e)
    {
        // INSCHRIJFGELD BEREKENEN
        // variabelen voor berekenen inschrijfgeld
        double club1Price;
        club1Price = 1.25;
        double club2Price;
        club2Price = 5.50;
        double club3Price;
        club3Price = 9.25;
        double price;
        price = 20.00;

        int value;
        value = Convert.ToInt16(ddlClass.SelectedValue.ToString());

        switch (ddlClub.SelectedItem.Text)
        {
            case "club 1":
                if (Double.TryParse(lblAmount.Text, out club1Price)) 
                    club1Total = club1Price * value;
                    string totalRoundClub2 = Convert.ToString(Math.Round(club2Total, 2, MidpointRounding.AwayFromZero));
                    lblAmount.Text = Convert.ToString(totalRoundClub2);
                break;
            case "club 2":
                if (Double.TryParse(lblAmount.Text, out club2Price)) 
                    culb2Total = club2Price * value;
                    string totalRoundClub2 = Convert.ToString(Math.Round(club2Total, 2, MidpointRounding.AwayFromZero));
                    lblAmount.Text = Convert.ToString(totalRoundClub2);
                break;
            case "club 3":
                if (Double.TryParse(lblAmount.Text, out club3Price)) 
                    club3Total = club3Price * value;
                    string totalRoundClub3 = Convert.ToString(Math.Round(club3Total, 2, MidpointRounding.AwayFromZero));
                    lblAmount.Text = Convert.ToString(totalRoundClub3);
                break;
            default:
                if (Double.TryParse(lblAmount.Text, out price)) 
                    total = price * value;
                    string totalRound = Convert.ToString(Math.Round(total, 2, MidpointRounding.AwayFromZero));
                    lblAmount.Text = Convert.ToString(totalRound);
                break;
        }
    }

The variables in my class:

public partial class Inschrijven : System.Web.UI.Page
{
    double club1Total;
    double club2Total;
    double club3Total;
    double total;

Can someone please help me?

Thanks in advance!

Black Frog
  • 11,595
  • 1
  • 35
  • 66
Marjolein
  • 193
  • 1
  • 12
  • debug and check the value you get for ddlClub.SelectedItem.Text – Damith Apr 19 '15 at 16:58
  • From the explanation it looks like its not retaining the selected position Read this answer here http://stackoverflow.com/questions/4189158/asp-net-dropdownlist-not-retaining-selected-item-on-postback – Praveen Paulose Apr 19 '15 at 17:03
  • I get all the selected values right but it seems to skip `total = price * value;` – Marjolein Apr 19 '15 at 17:51
  • I have done now `total = 20.00 * value;` And deleted `if (Double.TryParse(lblAmount.Text, out club2Price))` And that works fine so for some reason those two things give a problem... – Marjolein Apr 19 '15 at 18:04

1 Answers1

0

Set a breakpoint at

switch (ddlClub.SelectedItem.Text)

And inspect the value of ddlClub.SelectedItem.Text. I would suspect it's not one of the string in your case statements.

I would be better to use the valid of the select item in ddClub. For example: ddlClub.SelectedValue

Let's debug the following code:

// the if statement is going to run line below it because there
// is not curly braces to execute a block of code
if (Double.TryParse(lblAmount.Text, out club1Price)) 
    club1Total = club1Price * value;

string totalRoundClub2 = Convert.ToString(Math.Round(club2Total, 2, MidpointRounding.AwayFromZero));
lblAmount.Text = Convert.ToString(totalRoundClub2);

The Double.TryParse will set the club1Price to ZERO if the parse fails.

It should be written to the following:

if (Double.TryParse(lblAmount.Text, out club1Price))
{
    club1Total = club1Price * value;
}
else
{
    // the parse failed
    club1Price = 1.25;
    club1Total = club1Price * value;
}

string totalRoundClub2 = Convert.ToString(Math.Round(club2Total, 2, MidpointRounding.AwayFromZero));
lblAmount.Text = Convert.ToString(totalRoundClub2);
Black Frog
  • 11,595
  • 1
  • 35
  • 66
  • I have already set a breakpoint and it gets all the values but I don't get a value from price (and so total is also 0) and by every selection of the drop down list it will go to default... I think need to have SelectedItem.Text becasue I want to specify that club 1 need to pay 1.25 per person ect. But when another club is added in the database all the values of the drop down list will change because I have set it on ASC. So when cub 1 is now value 1 than it may be value 3 for example. – Marjolein Apr 19 '15 at 17:36
  • Do you know how I can change/ fix my code so it will give a value in my variables price and total? – Marjolein Apr 19 '15 at 17:37
  • Does "Club 1" in your database have a primary key? Why not use that as the value? So it doesn't matter what order the list is in. – Black Frog Apr 19 '15 at 17:41
  • That's right I didn't think of that :P I will try that! – Marjolein Apr 19 '15 at 17:45
  • I have done that now but lblAmount.Text is still 0... It seems to skip `club3Total = club3Price * value;` :S – Marjolein Apr 19 '15 at 17:47
  • Also another reason for using the primary id, is globalization. Your app may never use another language but you should practice not hardcoding string comparison. – Black Frog Apr 19 '15 at 17:47