What is the right way to load the values from the database to label?

Admin.aspx

       <div id="valueIntroduction" class="labelarea" runat="server">   </div>
       <div  class="line"></div>

Admin.aspx.cs

        SqlConnection NewConn = new SqlConnection(ConfigurationManager.ConnectionStrings["SoicConnection"].ConnectionString);
        NewConn.Open();
        SqlCommand NewComm = new SqlCommand();
        SqlCommand NewComm1 = new SqlCommand();

        if (Department.Items[0].Selected)
        {
                firstPanel.Visible = true;
                myLegend.InnerText = "Informatics";
                NewComm.CommandText = "getTextHeaderINFO";

                NewComm.CommandType = CommandType.StoredProcedure;
                NewComm.Connection = NewConn;
                NewComm.CommandTimeout = 3000;
                SqlDataReader results = NewComm.ExecuteReader();

                while (results.Read())
                {
                     Response.Write(results["TEXT_CONTENT"].ToString());
                     Label valueIntroduction = results["TEXT_CONTENT"];
                }

         }

What I am exactly tryting is to get the value from database and loading it into a label. I am new to .net and stackoverflow. Sorry incase if I dont know how to use this forum properly.

Upvotes: 1

Views: 137

Answers (1)

शेखर
शेखर

Reputation: 17614

use SqlDataReader results = NewComm.ExecuteSclar();

Instead of SqlDataReader results = NewComm.ExecuteReader();

And set the label text to the result.

   ResultLabel.Text = NewComm.ExecuteScalar().ToString();
   conn.Close();

Here is a smililar queston
Display SQL query result in a label in asp.net

Edit

For the first record

 int counter=0;
 while (results.Read())
 {
     if(counter++=0)
     {
          ResultLabel.Text = results["TEXT_CONTENT"].ToString();
          conn.Close();
          break;
     }

 }

Upvotes: 1

Related Questions