0

Hello, Working on this program as to where I need to cultivate this Message to retrieve both the First and Last Name. Working with Cookies with ASP.NET. A simple log in form.

Here is the code to just retrieving the First name that the user inputted; Yes, it does work for just the first name.

//get first name from session state and set welcome message if it exists
        string firstName = (string)Session["FirstName"];
        if (firstName != null)
        {
            lblWelcome.Text = "Welcome back, " + (string)Session["FirstName"] + "!";
        }

But as I mentioned, I actually need both of the first and last names. And here is my code that I used to try to work this out; but as it would be, it is not retrieving both of them, still just the first name.

//get firstname from cookie and set welcome message if it exists
        //HttpCookie cookie = Request.Cookies["FirstName"];
        //if (cookie != null)
        //    lblWelcome.Text = "Welcome back, " + cookie.Value + "!";

        //need to get first & last name from session state and set welcome message if it exists

        string firstName = (string)Session["FirstName"];
        string lastName = (string)Session["LastName"];
        if ((firstName != null) && (lastName != null))
        {
            lblWelcome.Text = "Welcome back, " + (string)Session["FirstName"] + (string)Session[" LastName"] + "!";
        }

This is the code behind on my Order.aspx page (snippet)

 protected void Page_Load(object sender, EventArgs e)
    {
        // get entry data from cookies
        //if (Request.Cookies["FirstName"] != null)
        //    txtFirstName.Text = Request.Cookies["FirstName"].Value;
        //if (Request.Cookies["LastName"] != null)
        //    txtLastName.Text = Request.Cookies["LastName"].Value;

        //get entry data from session state
        if (!IsPostBack)
        {
            string firstName = (string)Session["FirstName"];
            if (firstName != null) txtFirstName.Text = (string)Session["FirstName"];
            string lastName = (string)Session["FirstName"];  //my thoughts on this line is that the FirstName should be replaced with ["LastName"]
            if (lastName != null) txtLastName.Text = (string)Session["LastName"];
            txtFirstName.Focus();
        }       
    }    

Would anyone offer me some suggestions as to what I might be doing incorrect?

3
Contributors
2
Replies
18
Views
2 Years
Discussion Span
Last Post by JorgeM
0

Are you sure that Session["LastName"] is being set properly in the first place? I mean the cookie is being set properly? I think the only reason your new code wouldn't work is if Session["LastName"] is null.

And yes, the string lastName = (string)Session["LastName"]; replacement would be correct.

0

Yes line 14 should be LastName.

It maybe null or could be an empty string as suggested by Cj. Try this to see what the results are...

 if (string.IsNullOrWhiteSpace(lastname)) txtLastName.Text = "oops";

This will test if the value is null, empty, or white space. If it is then the condition is true and at least you will have identified the problem.

Edited by JorgeM

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.