Solved c# 2.0 bind to label?

Posted on 2006-06-05
Last Modified: 2008-02-01
How do I query SQL Server for a value, bind to a label and close all connections that need to closed?

protected void Page_Load(object sender, EventArgs e)
String OL = Request.QueryString["OL"];

SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["IntrConnection"].ConnectionString);
string strSQL = "select email_id from table where officeLoc = '" + OL + "' and title = 'RCA'";
SqlDataAdapter myCommand = new SqlDataAdapter(strSQL, myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "FP");
Source = new DataView(ds.Tables["FP"]);

//bind to label
LabelRCA.Text = DataSet1.Tables(0).Rows(0).Item("email_id").ToString();

//close connections


Or is there a way to bind to a variable?
Question by:fwsteal
    LVL 7

    Expert Comment

    Pretty sure you cant bind to a label in the way you want you can in a winforms app.

    for your connection you may want to use the using contruct, this will close and dispose the connection implicitly.  You may also want to consider using a datareader which is alot faster...and if you only want the one value back then you want to use ExecuteScalar on you sqlcommand object.

    using(SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["IntrConnection"].ConnectionString)) {
    // use connection in here with reader or adapter

    hope this helps


    Author Comment

    I get the using; but how would I bind to a label or a literal?
    LVL 7

    Accepted Solution

    like i said i dont think you can bind directly to a label that isnt within a template field on a gridview etc.

    using execute scalar would be done....

    using(sqlconnection....) {
      using(sqlcommand cmd = new sqlcommand("select name from table where id=1", conn)){
        Label1.Text = cmd.ExecuteScalar.ToString();

    or if you have the value in a datatable already you should get from there.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now