Question : Set the text in a label from a database

I have attached a fragment of code from Visual Web Developer 2005 (Express) which displays  a database field in a drop down menu.

All I want to do is to show the database field in an asp:label instead of a dropdownlist.

I think I need to use a datareader instead of the SQLDataSource but I don't know how to do that in this context.

Can anyone help please?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:

    
    
        
        
    
    
    

Answer : Set the text in a label from a database

assume you added a label named label1 on your page.
write this code in your page_load () :

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
System.Data.SqlClient.SqlConnection _cn = new System.Data.SqlClient.SqlConnection();
            _cn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
            _cn.Open(); 
            System.Data.SqlClient.SqlCommand SelectCommand = new System.Data.SqlClient.SqlCommand("SELECT AgentName FROM [Manager Database].dbo.tblAgents WHERE (AgentID = 45)", _cn); 

System.Data.SqlClient.SqlDataReader dtr;
            dtr = SelectCommand.ExecuteReader();
             
            if (dtr.Read())
label1.Text=dtr["AgentName"].ToString();
             
_cn.Close();
Random Solutions  
 
programming4us programming4us