user3234335
user3234335

Reputation: 47

Session not getting created/used

I wrote this code in which I made a session but I am not sure if the session is not getting created or is not getting used

    Session["user"] = this.txtUser.Text.Trim();

The page I am trying to get the session value from :

   protected void Page_Load(object sender, EventArgs e)
   {
    if (Session["user"] == null)
    {
        txtName.Text = Session["user"].ToString();
    }

Upvotes: 0

Views: 196

Answers (2)

Imad
Imad

Reputation: 7490

Change your code from

if (Session["user"] == null)
    {
        txtName.Text = Session["user"].ToString();
    }

to this

if (Session["user"] != null)
    {
        txtName.Text = Session["user"].ToString();
    }

in Page_Load

Because you are setting txtName's Text property when Session is null. It is not the case because session is having previous value of TextBox.

Upvotes: 3

AbdulRahman Ansari
AbdulRahman Ansari

Reputation: 3057

While fetching the session value, the code should be as following,

protected void Page_Load(object sender, EventArgs e)
    {
    if (Session["user"] != null)
    {
        txtName.Text = Session["user"].ToString();
    }

Upvotes: 1

Related Questions