Urgent ! Displaying Data From Db to Label

Posted by .Netlearner under ASP.NET on 4/18/2013 | Points: 10 | Views : 727 | Status : [Member] | Replies : 4
Hello All,

i have 6 label say labelname1,labelname2,labelname3,labelage1,labelage2,labelage3.and my db looks like

ID Name Age
-- -------- ------
1 aaa 20
2 bbb 25
3 ccc 22

i have read data through datareader and got data in array such as String[] name,String[] age.
int i=0;
while(dr.read)
{
name[i]=dr["name"].toString();
age[i]=dr["age"].toString();
i++;
}
labelname1=name[0].toString();
labelname2=name[1].toString();


its working fine but my question is , Is this the right code ? Can it can be done without creating an array. If yes how? Please help.




Responses

Posted by: Satyapriyanayak on: 4/18/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down

<%@ 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();

}
}
}

If this post helps you mark it as answer
Thanks

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 4/18/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
If this post helps you mark it as answer
Thanks

If this post helps you mark it as answer
Thanks

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 4/19/2013 [Member] Starter | Points: 25

Up
0
Down
yeah there is no need to display by taking in array, you can simply write
labelname1.Text=dr["name"].toString();

labelname2.Text=dr["age"].toString();

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: .Netlearner on: 4/19/2013 [Member] Starter | Points: 25

Up
0
Down
if i want to display one column value in one label i can give

labelname2.Text=dr["age"].toString();

but i want to display values in one column in different labels,

labelname1.Text=dr["age"].tostring();

labelname2.Text=dr["age"].toString();

but i want different age values

.Netlearner, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response