Get multiple data from table and show in different label boxes
-
Hi All, I have table with 3 fields id , status and Name... I have 3 or more label boxes in my header. I getting data like, "select id, Name from table1 where status = 1" My data are, id Name 1001 AAA 1002 CCC 1003 SSS My code is
Header head = new Header();
DataSet obj = new DataSet();
obj = head.getdata();
if (obj.Tables[0].Rows.Count < 0)
{
int i;
for (i = 0; i <= obj.Tables[0].Rows.Count - 1; i++)
{
string text1 = obj.Tables[0].Rows[i]["id"].ToString();
string text2 = obj.Tables[0].Rows[i]["Name"].ToString();
lblMessage.Text = text1 + " " + text2;
}
}It shows only the last data...I know i have to assign values to lblmessage1.text and lblmessage2.text and so on.. But i do not know how to fix it... I am fresher and new to sql....Give some samples Thanks in Advance
-
Hi All, I have table with 3 fields id , status and Name... I have 3 or more label boxes in my header. I getting data like, "select id, Name from table1 where status = 1" My data are, id Name 1001 AAA 1002 CCC 1003 SSS My code is
Header head = new Header();
DataSet obj = new DataSet();
obj = head.getdata();
if (obj.Tables[0].Rows.Count < 0)
{
int i;
for (i = 0; i <= obj.Tables[0].Rows.Count - 1; i++)
{
string text1 = obj.Tables[0].Rows[i]["id"].ToString();
string text2 = obj.Tables[0].Rows[i]["Name"].ToString();
lblMessage.Text = text1 + " " + text2;
}
}It shows only the last data...I know i have to assign values to lblmessage1.text and lblmessage2.text and so on.. But i do not know how to fix it... I am fresher and new to sql....Give some samples Thanks in Advance
Hi, You have told that you are using three or more label controls to display three rows picked from the database tables. As far as the code that you have posted, you have started a for loop and it binds the value with the lblMessage control. So it is pretty clear that the value which is getting bound to the lblMessage control, is getting overwritten each time the for loop runs. So at the end of the loop execution, the label is showing the last result only. :( Suppose, you have three controls like lblMessage1, lblMessage2 and lblMessage3. To achieve the functionality referred by you, just try the following code: for (i = 0; i<= obj.Tables[0].Rows.Count - 1; i++) { if(i==0) { string text1 = obj.Tables[0].Rows[i]["id"].ToString(); string text2 = obj.Tables[0].Rows[i]["Name"].ToString(); lblMessage1.Text = text1 + " " + text2; } if(i==1) { string text1 = obj.Tables[0].Rows[i]["id"].ToString(); string text2 = obj.Tables[0].Rows[i]["Name"].ToString(); lblMessage2.Text = text1 + " " + text2; } else { string text1 = obj.Tables[0].Rows[i]["id"].ToString(); string text2 = obj.Tables[0].Rows[i]["Name"].ToString(); lblMessage3.Text = text1 + " " + text2; } } All the best. :)
Either you love IT or leave IT...
-
Hi, You have told that you are using three or more label controls to display three rows picked from the database tables. As far as the code that you have posted, you have started a for loop and it binds the value with the lblMessage control. So it is pretty clear that the value which is getting bound to the lblMessage control, is getting overwritten each time the for loop runs. So at the end of the loop execution, the label is showing the last result only. :( Suppose, you have three controls like lblMessage1, lblMessage2 and lblMessage3. To achieve the functionality referred by you, just try the following code: for (i = 0; i<= obj.Tables[0].Rows.Count - 1; i++) { if(i==0) { string text1 = obj.Tables[0].Rows[i]["id"].ToString(); string text2 = obj.Tables[0].Rows[i]["Name"].ToString(); lblMessage1.Text = text1 + " " + text2; } if(i==1) { string text1 = obj.Tables[0].Rows[i]["id"].ToString(); string text2 = obj.Tables[0].Rows[i]["Name"].ToString(); lblMessage2.Text = text1 + " " + text2; } else { string text1 = obj.Tables[0].Rows[i]["id"].ToString(); string text2 = obj.Tables[0].Rows[i]["Name"].ToString(); lblMessage3.Text = text1 + " " + text2; } } All the best. :)
Either you love IT or leave IT...
Thanks for your reply.. I case i have mulitple records in my table and may use more label box in my page.. How can I change my code?
-
Thanks for your reply.. I case i have mulitple records in my table and may use more label box in my page.. How can I change my code?
-
Hi All, I have table with 3 fields id , status and Name... I have 3 or more label boxes in my header. I getting data like, "select id, Name from table1 where status = 1" My data are, id Name 1001 AAA 1002 CCC 1003 SSS My code is
Header head = new Header();
DataSet obj = new DataSet();
obj = head.getdata();
if (obj.Tables[0].Rows.Count < 0)
{
int i;
for (i = 0; i <= obj.Tables[0].Rows.Count - 1; i++)
{
string text1 = obj.Tables[0].Rows[i]["id"].ToString();
string text2 = obj.Tables[0].Rows[i]["Name"].ToString();
lblMessage.Text = text1 + " " + text2;
}
}It shows only the last data...I know i have to assign values to lblmessage1.text and lblmessage2.text and so on.. But i do not know how to fix it... I am fresher and new to sql....Give some samples Thanks in Advance
Hi, This is simple just include a plus(+)... Look at the code in which i did some alteration. Header head = new Header(); DataSet obj = new DataSet(); obj = head.getdata(); if (obj.Tables[0].Rows.Count < 0) { int i; for (i = 0; i <= obj.Tables[0].Rows.Count - 1; i++) { string text1 = obj.Tables[0].Rows[i]["id"].ToString(); string text2 = obj.Tables[0].Rows[i]["Name"].ToString(); // Just include a plus(+) lblMessage.Text += text1 + " " + text2; } }