Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi Sir,

I want that as user login the name of that user so on the top as:"Welcome username".
How will i do it?
Thank You.
Posted

Write below line on your .aspx page :

<asp:Label ID="lblUserName" runat="server" Text="Welcome"></asp:Label>

And write Code in .cs Page :

First Execute query which will check your login and will return you a username of Authorized user and just apply this username to below line,


lblUserName.Text = lblUserName.Text + " " + username;

I hope it will help you.
 
Share this answer
 
in SQL command you check if user name and pass are valid, get Full name of user and save to variable(using query)and if you want only display username without Fullname, you also save this username that user login and then set it to a label or any control which you like. I have some code line to you reference:
// connection string
string fullname, nhom,sex;
string strketnoi = "SELECT UserName,UserID,Pass,USERS.Type,EmpName,Sex FROM USERS,Employee WHERE UserID=EmpID";
SqlCommand sqlcmd = new SqlCommand(strketnoi,sqlconn);
sqlconn.Open();
SqlDataReader sqlread = sqlcmd.ExecuteReader();
while (sqlread.Read())
{
    username = sqlread.GetString(0);
    pass = sqlread.GetString(2);
    if (username.Trim() == txtusername.Text.Trim())
    {
        if (pass.Trim() == txtpass.Text.Trim())
        {
            EmpID = sqlread.GetInt32(1);
            type = sqlread.GetString(3);
            tenEmp = sqlread.GetString(4);
            sex = sqlread.GetString(5);
           // Save info of user to sessions to use to other purpose
            Session["empid"] = EmpID.ToString();
            Session["hoten"] = tenEmp.Trim();
            Session["role"] = type.Trim();
            Session["Sex"] = sex.Trim();
            sqlconn.Close();
            Response.Redirect("Home.aspx");
        }
    }
}
sqlconn.Close();
 fullname=tenEmp ;
lbHello.Visible = true;
lbName.Visible = true;
if (sex.Trim() == "B")
{
    lbHello.Text = "Hello Mr ";
}
else
{
    lbHello.Text =  Hello Ms;
}
lbName.Text = fullname;
 
Share this answer
 
v3
When u authenticate a user using login form,then store its name in the session.
Once the session started,u can retrieve its value anywhere until it closes.

XML
<table >
           <tr>
               <td>
                               Email Address</td>
           </tr>
           <tr>
               <td>
                               <asp:TextBox ID="textemail" runat="server" Width="300px"></asp:TextBox>
                           </td>
           </tr>
           <tr>
               <td>
                               &nbsp;</td>
           </tr>
           <tr>
               <td>
                               Password</td>
           </tr>
           <tr>
               <td>
                               <asp:TextBox ID="textpass" runat="server" Width="300px"
                       TextMode="Password"></asp:TextBox>
                           </td>
           </tr>
           <tr>
               <td>

                   &nbsp;</td>
           </tr>
           <tr>
               <td>

                   <asp:Button ID="buttonlogin" runat="server" Text="Login"
                       onclick="buttonlogin_Click" />

               </td>
           </tr>
           <tr>
               <td>

                   &nbsp;</td>
           </tr>
           </table>


C#
class page
{
SqlConnection con;
SqlCommand cmd;
SqlDataReader dr;
protected void Page_Load()
{
 con = new SqlConnection("data source=servername;initial catalog=databasename;integrated security=true");
                con.Open();
}


protected void buttonlogin_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            
                

                cmd = new SqlCommand("select email,password from login where email=@email and 
password=@password" , con);
cmd.Parameters.AddWithValues("@email",textemail.Text);
cmd.Parameters.AddWithValues("@email",textpass.Text);
                dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                       Session["name"]=dr.GetString(0);
                       Response.Redirect("userprofile.aspx");
                    }
                    dr.Close();
                }
}
}
}



and when you redirected to the new userprofile page.Add a label on that page and write this code at page_load

label1.text=Session[name].ToString();

OKK
 
Share this answer
 
v2
Comments
SURBHI TYAGI 22-Apr-11 5:38am    
but if i do login through credentials then how i will show the name of user?
Put a Lable control on the page (which may be invisible initially). On successful log in, get the name of the user from the database and assign it to the label's text. Where's the problem?
 
Share this answer
 
v2

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