Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a little problem. I wanted to know why is it when I login the new session will not display the user name? I have the code on another computer and I ran the debug on that computer and I was able to log in and the Label displayed the username. Here is the code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CEOPage : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["New"] != null)
        {
            Label1.Text += Session["New"].ToString();
        }
        else
        {
            Response.Redirect("Login.aspx");
        }
    }
     protected void Button1_Click(object sender, EventArgs e)
    {
        Session["New"] = null;
        Response.Redirect("Login.aspx");
    }
}


I ran the debug on the code and kept getting redirected back to the login. So I changed the code to this:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CEOPage : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
     protected void Button1_Click(object sender, EventArgs e)
    {
        Session["New"] = null;

        if (Session["New"] != null)
        {
            Label1.Text += Session["New"].ToString();
        }
        else
        {
            Response.Redirect("Login.aspx");
        }
    }
}

The Login works without redirecting back to login page but on the welcome page the label is not showing the user name. What did I do wrong and what am I missing?
Posted
Comments
Jignesh Khant 24-Jun-13 9:03am    
Store the user name from login Page in a session and retrieve that session on welcome page. Something like this:
// on login page:
session["UserName"]=txtusername.ToString();

// On welcome page.

lblusername.Text=session["UserName"].ToString();
Computer Wiz99 24-Jun-13 9:52am    
Ok. Thanks. Could this work in the page load on the welcome page?

1 solution

Hi Kwesi,

Looks like you need to check the logic of the code. The logic in the below code is like you are setting the session to null always (after every button click). I was wondering, why is there, a need to use the if condition.

protected void Button1_Click(object sender, EventArgs e)
    {
        Session["New"] = null;
 
        if (Session["New"] != null)
        {
            Label1.Text += Session["New"].ToString();
        }
        else
        {
            Response.Redirect("Login.aspx");
        }


During the page load, you can assign the session to a label if you are redirecting from the Login page to the Welcome page. (As per Jignesh Khart's comment)

Thanks,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900