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:-
01 | <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> |
10 | < form id = "form1" runat = "server" > |
15 | < asp:Label ID = "Label1" runat = "server" Text = "" ></ asp:Label ></ td > |
17 | < asp:TextBox ID = "TextBox1" runat = "server" ></ asp:TextBox > </ td > |
Code View:-
02 | using System.Collections.Generic; |
06 | using System.Web.UI.WebControls; |
08 | using System.Data.SqlClient; |
10 | public partial class _Default : System.Web.UI.Page |
12 | protected void Page_Load( object sender, EventArgs e) |
14 | SqlConnection con = new SqlConnection( "Data Source=hightech;Initial Catalog=session; User ID=sa; password=sa" ); |
16 | String str = "select ename,job from emp" ; |
17 | SqlCommand cmd = new SqlCommand(str, con); |
19 | SqlDataReader dr = cmd.ExecuteReader(); |
22 | TextBox1.Text = dr[ "ename" ].ToString(); |
23 | Label1.Text = dr[ "job" ].ToString(); |
Enjoy…