0

I want to display username in the top of all page.

Below is my code .

protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void btnLogin_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from Userman where Username =@username and Password=@password", con);
        cmd.Parameters.AddWithValue("@username", txtUsername.Text);
        cmd.Parameters.AddWithValue("@password", txtPassword.Text);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            Admin();
        }
        else
        {
            ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
        }
    }


    private void Admin()
    {
        if (txtUsername.Text == "admin" && txtPassword.Text == "123")
        {
            Response.Redirect("AdminHome.aspx");
        }
        else
        {
            Response.Redirect("UserHome.aspx");
        }
    }

Could you please help me on this

5
Contributors
4
Replies
29
Views
3 Years
Discussion Span
Last Post by ExpertMind
0

You will need a bit more code..

Here is an idea, use a master page and place a label somewhere on the page. In the master page's code behind, page load, check to see if your user has been authenticated. You can do this by storing some information in a cookie or session variable. At the page load, if you find the information, fill in the text of that label. If it's not, redirect the user to the login page

Asp.net has quite a bit of classes and controls already ready for you. I'd recommend you go to asp.net and read up on some of their tutorials especially in the authentication section/chapter. It could save you a lot of time.

0

As our above said, put a Label on White in the place you want to stay, and check if authentication was performed Login if Yes complete the Label with the value of the User Name, if not let the Blank Label. display or change the style to None.

0

follow below steps
1) use master page
2) add a label for showing username
3) after login save the username in session
4) Assign session value to the label in which you need to show username

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.