0

I have a drop down list where I stored 3 elements inside it.

<asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="True" 
                Height="22px" Width="134px">
                <asp:ListItem>Please Choose</asp:ListItem>
                <asp:ListItem>Yes</asp:ListItem>
                <asp:ListItem>No</asp:ListItem>
            </asp:DropDownList>

When user choose Yes in drop down, the YES will be stored in session. Chauffeur service will charge user $30.

    Session["IsChauffeurUsed"] = DropDownList4.SelectedItem.Selected;

The thing is what to do if user select YES the the current amount that user have to pay is

    int totalValue = 0;
    int total = 0;
    totalValue = int.Parse(Session["price"].ToString()) * int.Parse(Session["day"].ToString()); 
    Label8.Text = totalValue.ToString();

*Label8.text will be plus 30 (if user select yes)

I got stuck at the bold line. I dont know how to add 30 in totalValue if user select YES I hope u guys can help me. Cheers.

Stuck
  • 229
  • 1
  • 3
  • 11

2 Answers2

2

You have to check you session value (If needed Add check for null) and then conditionally add the value

If (Session["IsChauffeurUsed"].ToString() == "Yes")
      totalValue +=30 ;
Label8.Text = totalValue.ToString();
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
1

Despite the fact that i find it horrible to take something, call ToString() on it and afterwards Type.Parse() on this string you could solve the problem as follows:

totalValue = int.Parse(Session["price"].ToString()) * int.Parse(Session["day"].ToString()); 
totalValue += Session["IsChauffeurUsed"].ToString().Equals("yes", StringComparer.CurrentCultureIgnoreCase) ? 30 : 0;
Oliver
  • 43,366
  • 8
  • 94
  • 151
  • Hi @Oliver, I get this error when applying your code. Member 'object.Equals(object, object)' cannot be accessed with an instance reference; qualify it with a type name instead – Stuck Aug 02 '12 at 07:20
  • @Stuck: Must be something with your `Session` or sth else. This dummy doesn't throw any errors: `int test = "test".ToString().Equals("yes", StringComparison.CurrentCultureIgnoreCase) ? 30 : 0;` – Oliver Aug 02 '12 at 07:26