0

I have tried setting my session values, but every time I want to display the text a user has entered in the previous page it returns null

I have three pages Subscriber details, Package Selection and Bank Details, all three pages follow each other and the user has filled all pages with his/her desired info and clicks next, they are directed to a page where all the info they entered are displayed.

I cant seem too see what I'm doing wrong?

SubscriberDetails.aspx

protected void Button1_Click(object sender, EventArgs e) { string FullName = txtFullName.Text; string CompanyName = txtCompanyName.Text; string Vat = txtVAT.Text; string ContactNumber = txtContactNumber.Text; string Fax = txtFax.Text; string District = txtDistrict.Text; string Street = txtStreet.Text; string City = txtCity.Text; string Code = txtPostal.Text; string Trading = txtTrading.Text; string Id = txtID.Text; string ContactPerson = txtContactPerson.Text; string Email = txtEmail.Text; Session["FullName"] = FullName; Session["CompanyName"] = CompanyName; Session["VAT"] = Vat; Session["ContactNumber"] = ContactNumber; Session["Fax"] = Fax; Session["District"] = District; Session["City"] = City; Session["Street"] = Street; Session["Code"] = Code; Session["Trading"] = Trading; Session["ID"] = txtID.Text; Session["ContactPerson"] = ContactPerson; Session["Email"] = Email; } 

Final.aspx

protected void Page_Load(object sender, EventArgs e) { if (string.IsNullOrEmpty((string)Session["Fullname"])) { lblFullName.Text = "N/A"; } else { lblFullName.Text = Session["FullName"].ToString(); } if (string.IsNullOrEmpty((string)Session["CompanyName"])) { lblCompanyName.Text = "N/A"; } else { lblCompanyName.Text = Session["CompanyName"].ToString(); } if (string.IsNullOrEmpty((string)Session["VAT"])) { lblVat.Text = "N/A"; } else { lblVat.Text = Session["VAT"].ToString(); } if (string.IsNullOrEmpty((string)Session["ContactNumber"])) { lblContactNumber.Text = "N/A"; } else { lblContactNumber.Text = Session["ContactNumber"].ToString(); } if (string.IsNullOrEmpty((string)Session["Fax"])) { lblFax.Text = "N/A"; } else { lblFax.Text = Session["Fax"].ToString(); } if (string.IsNullOrEmpty((string)Session["District"])) { lblDistrict.Text = "N/A"; } else { lblDistrict.Text = Session["District"].ToString(); } if (string.IsNullOrEmpty((string)Session["Street"])) { lblStreet.Text = "N/A"; } else { lblStreet.Text = Session["Street"].ToString(); } if (string.IsNullOrEmpty((string)Session["City"])) { lblCity.Text = "N/A"; } else { lblCity.Text = Session["City"].ToString(); } if (string.IsNullOrEmpty((string)Session["Code"])) { lblCode.Text = "N/A"; } else { lblCode.Text = Session["Code"].ToString(); } if (string.IsNullOrEmpty((string)Session["Trading"])) { lblTrading.Text = "N/A"; } else { lblTrading.Text = Session["Trading"].ToString(); } if (string.IsNullOrEmpty((string)Session["ID"])) { lblID.Text = "N/A"; } else { lblID.Text = Session["ID"].ToString(); } if (string.IsNullOrEmpty((string)Session["ContactPerson"])) { lblContactPerson.Text = "N/A"; } else { lblContactPerson.Text = Session["ContactPerson"].ToString(); } if (string.IsNullOrEmpty((string)Session["Email"])) { lblMail.Text = "N/A"; } else { lblMail.Text = Session["Email"].ToString(); } } 
2
  • Is string FullName = txtFullName.Text; returning null, or is Session["Fullname"] returning null? Commented Apr 10, 2013 at 21:15
  • Not an answer, but perhaps look into the ternary operator. This would simplify your code and you could have something like lblContactPerson.Text = string.IsNullOrEmpty((string)Session["ContactPerson"]) ? "N/A" : Session["ContactPerson"].ToString(); so that you don't have pages of if statements. Commented Apr 10, 2013 at 21:20

2 Answers 2

2

Well you don't have many options there, if that is happening you have an issue with your session. Perhaps every other request is initiating a new session, check that to make sure it is not happening.

Check with:

Session.SessionID; 
Sign up to request clarification or add additional context in comments.

Comments

0

I see one piece of code using TextBox controls and the lower using Label controls.

You aren't, maybe, reading the wrong ones, are you?

This as just an FYI: We get customer complaints on our websites if we fill in values like "N/A". Most people seem to prefer to see the blank spaces, which would also simplify your code:

lblVariable.Text = string.Format("{0}", Session["Variable"]); 

Variable, obviously, needs to change with your Session and Control variable names.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.