How To Bind Data To Textbox and Label In ASP.NET

How To Bind Data To Textbox and Label In ASP.NET

In this post i will explain you How To Bind Data To Textbox and Label In ASP.NET.Here i am using SQL Server as backend from where i am fetching data and then binding that data into Textbox and Label.

Design View:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label></td>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td>

Code View:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        SqlConnection con = new SqlConnection("Data Source=hightech;Initial Catalog=session; User ID=sa; password=sa");

        String str = "select ename,job from emp";
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
            TextBox1.Text = dr["ename"].ToString();
            Label1.Text = dr["job"].ToString();