Monday 5 August 2013

How to read a data from database and display in a Label

HI friends today i shows how to read data from table and display in a table. Read our previous posts:
How to Load Checklist With Database Table Value and
How to insert value into a table in ASP.net (
 Database :








Design :







Code: 
    <form id="form1" runat="server">
    <div>
   
        <br />
        Your Phone number is:
           <asp:Label ID="Label1" runat="server"></asp:Label>
           <br /><asp:Button ID="Button1" runat="server" onclick="Button1_Click"
     </div>
    </form>
.CS page 
  protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=JISHNU\\SQLEXPRESS;Initial Catalog=dreamtheweb;Integrated Security=True");
          
            SqlCommand phone = new SqlCommand("select phone from account where id='1' ", con);
            con.Open();
            decimal ph = (decimal)phone.ExecuteScalar();
            Label1.Text = Convert.ToString(ph);

            con.Close();        
        }

Note: phone number is a numeric type so convert first .
          If you get name from database you can directly insert to label without Convert.ToString()

0 comments:

Post a Comment