0

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.

yogi
  • 19,175
  • 13
  • 62
  • 92
Stuck
  • 229
  • 1
  • 3
  • 11
  • 1
    How does the user select whether he wants that service or not? just hold another session variable for that. – Shai Aug 01 '12 at 07:30
  • I provide it in dropdown list. User can select yes or No – Stuck Aug 01 '12 at 07:32
  • Then save the dropdown list's selected option in session also and use it in next page. – Narendra Aug 01 '12 at 07:34
  • 1
    Using Session variables in this manner seems like you need to rethink your design (unless it can't be avoided). I also suggest renaming your identifiers. Label1, Label2 means nothing. – Lews Therin Aug 01 '12 at 07:35

5 Answers5

4

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.

Umesh
  • 2,704
  • 19
  • 21
3

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();
Vinoth
  • 2,419
  • 2
  • 19
  • 34
0

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

Narendra
  • 3,069
  • 7
  • 30
  • 51
0

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
Zo Has
  • 12,599
  • 22
  • 87
  • 149
  • Status of Chauffer can be either true or false so you can use a boolean & store that in Session then check the session & add 30 dollars where necessary. Can you pinpoint exactly what you don't understand? – Zo Has Aug 01 '12 at 08:49
  • Act I dont know how to write the code in my system. This is so embarrassing. – Stuck Aug 01 '12 at 08:56
0

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.

Community
  • 1
  • 1
Jigar Pandya
  • 6,004
  • 2
  • 27
  • 45