Click here to Skip to main content
15,902,939 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
str = "select CAT_NO from CATEGORY1";
cmd = new SqlCommand(str, cn);
sda = new SqlDataAdapter(cmd);
ds = new DataSet();
sda.Fill(ds, "CATEGORY1");
dt = ds.Tables[0];
foreach (DataRow dr1 in dt.Rows)
{
    label1.Text = dr1[0].ToString();
    label2.Text = dr1[0].ToString();
}

The table contains two rows sub1 and sub2 and the label1 should display sub1 and label 2 display sub2 but using this code both the labels displays sub2
Posted
Updated 20-Apr-15 21:19pm
v2
Comments
Deepak Kanswal Sharma 21-Apr-15 3:21am    
Have you tried changing the index?
label2.Text = dr1[1].ToString();
MadhuRavuri 21-Apr-15 3:24am    
yes when changing the index the error was cannot find column 1
MadhuRavuri 21-Apr-15 3:25am    
how to get 2nd row value of same column to another label
Deepak Kanswal Sharma 21-Apr-15 3:39am    
What about the rows syntax?

Can you try changing the label2.Text assignment to this:
C#
label2.Text = dr1[1].ToString();

Oh I see now (or at least I hope I know now what you mean), you have two rows with a single column (that CAT_NO column) right?
In that case try this:
C#
dt = ds.Tables[0];
label1.Text = dt.Rows[0][0].ToString();
label2.Text = dt.Rows[1][0].ToString();
 
Share this answer
 
v2
Comments
MadhuRavuri 21-Apr-15 3:26am    
yes when changing the index the error was cannot find column 1
how to get 2nd row value of same column to label2
Mario Z 21-Apr-15 3:50am    
I edited my answer, please check it out.
I hope this helps.
label1.Text = dr1.Rows[0]["column-name"].ToString();
label2.Text = dr1.Rows[1]["column-name"].ToString();

Try this thing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900