Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
i have been developing a website using asp.net web form.after authentication i need the logged in user's name in a label.
i tried the following code and query to display but failed..
-----------
C#
protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con= new SqlConnection();
        con.ConnectionString=@"Data Source=KAARTHICK-PC\KAARTHICKRAMAN;Initial Catalog=sample;Integrated Security=True";
        con.Open();
        string query="select UserName from Login123 where UserName='"+Label2.Text+"' ";
        SqlDataAdapter ada=new SqlDataAdapter(query,con);
        DataTable dt=new DataTable();
        ada.Fill(dt);
        con.Close();
    }

----------
it will be grateful if you help me out. thanks in advance !
Posted
Updated 22-Sep-14 6:19am
v2
Comments
[no name] 22-Sep-14 12:09pm    
What does "failed" mean to you? Debug your code. What is Label2.Text? Why are you filling a datatable with the result and then not doing anything with it? Why would you use a datatable for a single string anyway?
Member 1097736 22-Sep-14 12:29pm    
fail means i coudn't retrieve the data and display the name in label!
ther may be 'n' no of users who may login..so displaying their name on Label2 after authorizing is what i need.
like gmail,fb i need to display particular user info at top right corner after they login!
provide me a sol
[no name] 22-Sep-14 12:37pm    
Well why couldn't you? Your whole question makes no sense. You already have the user name from when the user logged in, so why do you need to query it out the database to begin with? You are not assigning anything to Label2. It's a page load event so Label2.Text is likely empty. So are you copying and pasting code from somewhere and hoping that it will work for you? And then when it doesn't work you have no idea why?
ChauhanAjay 22-Sep-14 12:41pm    
He has gone wrong there I suppose. He should have put the textbox value instead of the label value. After retrieving the data he should have populated the text.
Member 1097736 22-Sep-14 12:43pm    
new to this..so got confused!

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=KAARTHICK-PC\KAARTHICKRAMAN;Initial Catalog=sample;Integrated Security=True";
            using (con)
            {               
                con.Open();
                string query = "select UserName from Login123 where UserName='" + Your username text box.Text + "' ";
                SqlCommand cmd = new SqlCommand(query, con);
                string strUsrNm =Convert.ToString( cmd.ExecuteScalar());
                Label2.Text = strUsrNm;
                con.Close();
            }
        }
        catch
        {
        }
        finally
        {
            
        }
    }


Change your code like this. you should not use data table if it is not necessary.
 
Share this answer
 
Comments
Member 1097736 22-Sep-14 13:09pm    
but the textbox where i enter username is in another aspx page.
it throws error:
The name 'TextBox1' does not exist in the current context.
[no name] 22-Sep-14 21:43pm    
see my another answer. For your need its better to use session variable.
Exploring Session in ASP.NET[^]

For your need its better to use session variable.
 
Share this answer
 
Hi
Better way is after successful login you can store user name to a Session variable and
use this variable to set Text property of the label like this

Session["username"] = txtUser.Text;


where txtUser is the textbox in login page where you enter user name

then in the inner pages (or in the master page) you can set label text as below

lblUser.Text = Session["username"].ToString();
 
Share this answer
 
Comments
Member 1097736 23-Sep-14 8:57am    
i created session in login.aspx where i do authorization!
in login.aspx
----------------
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session["username"] = TextBox1.Text;

}
protected void Button1_Click(object sender, EventArgs e)
{
//code statements
}
}
-----------------------------------------
in entrypage..aspx where login.aspx directs after authenticating i allocated
Label2.Text = Session["username"].ToString;

but it throws error-"cannot covert method group'ToString' to non-delegate type 'string'"


public partial class entry_page_ : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label2.Text = Session["username"].ToString;
}
}
Member 1097736 23-Sep-14 9:07am    
praveen ji thanx! it worked :)
Praveen_P 24-Sep-14 2:08am    
:) thank you
Praveen_P 23-Sep-14 9:02am    
ToString is a method you didnot use function bracket, so use () after ToString , like following

lblUser.Text = Session["username"].ToString();

Member 1097736 23-Sep-14 9:07am    
i got it ! made a mistake..thanx again :)
You will need to check username from the textbox and not from the label.
After retrieving the data basing on the username you can display in the name in the label.

C#
protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con= new SqlConnection();
        con.ConnectionString=@"Data Source=KAARTHICK-PC\KAARTHICKRAMAN;Initial Catalog=sample;Integrated Security=True";
        con.Open();
        string query="select UserName from Login123 where UserName='"+ TextBox1.Text +"'";
        SqlDataAdapter ada=new SqlDataAdapter(query,con);
        DataTable dt=new DataTable();
        ada.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            Label2.Text = dt.Rows[0]["UserName"].ToString();
        }
        con.Close();
    }
 
Share this answer
 
v2
Comments
Member 1097736 22-Sep-14 12:52pm    
but the textbox where i enter username is in another aspx page.
it throws error:
The name 'TextBox1' does not exist in the current context.
ChauhanAjay 22-Sep-14 12:57pm    
TextBox1.Text was just a sample. Please use the name of the textbox in which you collect the username from the user.

What is the name of the page where you written the above code.
Member 1097736 22-Sep-14 13:00pm    
i have used the same default name for textbox-'TextBox1'!
the page name where i collect username to authenticate is Login.aspx
ChauhanAjay 22-Sep-14 13:03pm    
The page_load event in which page is it?
Member 1097736 22-Sep-14 13:04pm    
entry page..aspx
First off all take label control on a web form and set its ID such as lbl_session

I have a code for this i mentioned this code in master page page load event

here is my code

C#
if (Session["uname"] != null)
        {
            lbl_Session.ForeColor = Color.Green;
            lbl_Session.Text = Session["uname"].ToString();
        }
        else
        {
            Response.Redirect("lockscreen.aspx");
        }


I hope this will work for you
 
Share this answer
 
Using Session variable is the simple solution for you

During login you check the user name and password are avilable in db (this is ususl way) when you check the login crediatial simply store the user name into session
call the session where you want to display
 
Share this answer
 
Comments
Member 1097736 23-Sep-14 9:00am    
i created session in login.aspx where i do authorization!
in login.aspx
----------------
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session["username"] = TextBox1.Text;

}
protected void Button1_Click(object sender, EventArgs e)
{
//code statements
}
}
-----------------------------------------
in entrypage..aspx where login.aspx directs after authenticating i allocated
Label2.Text = Session["username"].ToString;

but it throws error-"cannot covert method group'ToString' to non-delegate type 'string'"


public partial class entry_page_ : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label2.Text = Session["username"].ToString;
}
}
[no name] 23-Sep-14 9:37am    
syntax vice its wrong dudue Label2.Text = Session["username"].ToString;

Label2.Text = Session["username"].ToString();

insted of ToString() try to use Convert.tostring() not only in here

dude get Session["username"] value from database

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