Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Can I display data from from sql server table into a label.text?
I am making a registration form followed by a login form.
The registration form takes values from textboxes and for a security question I have a combo box. So in registration and login I have succeeded in that but I want to change the user password so I first want to display the security question into a label's text. Can I do that?

[edit]SHOUTING removed - OriginalGriff[/edit]
Posted
Updated 25-Aug-12 23:48pm
v2
Comments
OriginalGriff 26-Aug-12 5:48am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.

To chose both wrong styles in the same question just makes you look like a rude child.
sariqkhan 26-Aug-12 5:57am    
oops sorry I dont know the rules
: )

1 solution

sure you can do that.
Steps:
1)Create connection object
2) open connection
3) create sqlcommand to read the data by passing the select query
4)execure reader
5) then labelname.text=Reader["Columnname"].ToString();


C#
try
{
    SqlDataReader myReader = null;
    SqlCommand    myCommand = new SqlCommand("select * from table", 
                                             myConnection);
    myReader = myCommand.ExecuteReader();
    while(myReader.Read())
    {
        label.text=myReader["Column1"].ToString();

    }
}
catch (Exception e)
{
    messagebox.show(e.ToString());
}





you can find more at
Beginners guide to accessing SQL Server through C#[^]
 
Share this answer
 
v2
Comments
Mohamed Mitwalli 26-Aug-12 6:06am    
5+
nitin bhoyate 26-Aug-12 6:08am    
thanks go ahead....do more coding....
sariqkhan 26-Aug-12 6:43am    
+5:)
sariqkhan 26-Aug-12 6:33am    
bro you have given column1 name. So it is not dynamically change when the user gets change. Sorry i have not specified before. But if i have a username column too. Then inserting a username the specified column beside that will come as label's text. Can we do that?
nitin bhoyate 26-Aug-12 6:44am    
if your table contains the column username then you can pass this instead of column1. and label will get content of that column.
Just try it. After trying you can understand more

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