In this blog we will know how to Display Data from Database to Label.
<%@
Page Language="C#"
AutoEventWireup="true"
CodeBehind="Default.aspx.cs"
Inherits="Displaying_Data_From_Db_to_Label._Default"
%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"
>
<head
runat="server">
<title>Untitled
Page</title>
</head>
<body>
<form
id="form1"
runat="server">
<div>
<asp:Label
ID="labelname1"
runat="server"
Text="Label"></asp:Label>
<asp:Label
ID="labelage1"
runat="server"
Text="Label"></asp:Label><br
/>
<asp:Label
ID="labelname2"
runat="server"
Text="Label"></asp:Label>
<asp:Label
ID="labelage2"
runat="server"
Text="Label"></asp:Label><br
/>
<asp:Label
ID="labelname3"
runat="server"
Text="Label"></asp:Label>
<asp:Label
ID="labelage3"
runat="server"
Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
using
System;
using
System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
using
System.Data.SqlClient;
namespace
Displaying_Data_From_Db_to_Label
{
public partial
class _Default
: System.Web.UI.Page
{
string strConnString =
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void
Page_Load(object sender,
EventArgs e)
{
SqlConnection con =
new SqlConnection(strConnString);
con.Open();
str = "select * from emp";
com = new
SqlCommand(str, con);
SqlDataReader reader =
com.ExecuteReader();
reader.Read();
labelname1.Text = reader["Name"].ToString();
reader.Read();
labelname2.Text = reader["Name"].ToString();
reader.Read();
labelname3.Text = reader["Name"].ToString();
reader.Close();
con.Close();
con.Open();
SqlDataReader reader1 =
com.ExecuteReader();
reader1.Read();
labelage1.Text = reader1["Age"].ToString();
reader1.Read();
labelage2.Text = reader1["Age"].ToString();
reader1.Read();
labelage3.Text = reader1["Age"].ToString();
reader.Close();
con.Close();
}
}
}


User_ 123002Posted Feb 27, 2021, 6:44 AM EST
Very well....
indradeo kumarPosted Jul 27, 2019, 8:04 PM EDT
Thanks dear ..
Pintoo YadavPosted Jan 21, 2016, 8:41 PM EST
nice one
Rana SunnyPosted May 15, 2015, 7:59 AM EDT
Thanks.....its very Help Full
kevin krishPosted Apr 6, 2015, 6:44 AM EDT
Nice One :-)
anil gaddePosted Nov 30, 2014, 8:43 PM EST
i have 7 records in my page how can i display that 7 records in seven labels using for loop?
soundara rajanPosted Oct 7, 2014, 9:32 PM EDT
Thanks Satyapriya really helpful :-)
huzefa petitPosted Jun 29, 2014, 10:08 PM EDT
very nice and helpful article
Rishabh AgrawalPosted Jul 2, 2013, 2:22 AM EDT
Thanku .. :) It is very help full :)