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?

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.