Stuck
Stuck

Reputation: 259

Calculation in code behind

Chauffeur service cost $30. It is an option to user whether they want the service or not. When they already select car, and day of rental, I store it in session.

Session["car1"] = Label1.Text;
Session["price"] = Label5.Text;
Session["day"] = DropDownList3.SelectedItem.Text;
Session["driver"] = Label6.Text;
Response.Redirect("~/Welcome/BookingForm.aspx");

Then in bookingform.aspx, I retrieve back the session, and display it in label.

string car1 = Session["car1"].ToString();
string price = Session["price"].ToString();
string day = Session["day"].ToString();
string driver = Session["driver"].ToString();

To calculate the total amount of user should pay, I used this code:

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

The problem now is, I don't know how to add $30 for chauffeur service IF user pick that service. Like if user choose yes, totalValue + 30, and if NO, do nothing.

Thanks for your time. Cheers.

Upvotes: 0

Views: 1095

Answers (5)

Vinoth
Vinoth

Reputation: 2439

Store the dropdown value into another session variable say IsChaffeurUsed. Make it a boolean.

bool IsChauffeurUsed= dropDown.SelectedValue=="Yes" ? true :false;
Session["IsChauffeurUsed"]= IsChauffeurUsed;


//Your logic to compute total

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

//check if chaffeur service is used

bool isChaffeurUsed = (bool) Session["IsChaffeurUsed"]; 

    if(isChaffeurUsed)
        {
        total += 30;
        }
Label8.Text = (totalValue).ToString();

Upvotes: 3

Jigar Pandya
Jigar Pandya

Reputation: 5977

totalValue = int.Parse(Session["price"].ToString()) * int.Parse(Session["day"].ToString()) + ShowFerInUse ? 30 : 0;

This way you can use iif equivalent in c# and do ShowFerInUse at same time.

Upvotes: 0

Umesh
Umesh

Reputation: 2732

Why don't you create a custom class, add the properties like Car, Price, Day, ChaffterUsed etc. Create the object of the class, fill the properties and store the object into session. On next page, load the object from session and use it.

Upvotes: 4

Zo Has
Zo Has

Reputation: 13018

When you provide to select the service in a dropdown, why not store that status in session as well ? Like

bool chauffeurService= YourServiceDropDown.SelectedValue=="1" ? true :false;
Session["chauffeurStatus"]=chauffeurService;

Then inside your form where to process values

// Check if Chauffer service status is 1 add 30 dollars else don't add anything

Upvotes: 0

Narendra
Narendra

Reputation: 3117

I suggest use Server.Transfer() instead of Response.Redirect() and use PreviousPage to fetch your required values.

This will save you from using Session as Session should not be used in this scenario.

To know how to use PreviousPage see below link.

http://msdn.microsoft.com/en-us/library/system.web.ui.page.previouspage.aspx

Upvotes: 0

Related Questions