label control is not displayed on winform c#
label control is not displayed on winform c#
I have this code where I have to fetch 5 records in array from database. I have to display each record on each label. So I have generated runtime labels, used split function to split the array and then display each one of them on each label. When I run this code and click the button only one value is displayed. What is wrong with this code?
private void button1_Click(object sender, EventArgs e)
{
for(int i=0;i<5;i++)
{
addlabel(i);
}
for (int i1 = 0; i1 < 5; i1++)
{
addlabel1(i1);
}
}
void addlabel(int i)
{
string cons = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
SqlConnection con = new SqlConnection(cons);
con.Open();
string str = "SELECT * FROM marks WHERE idno = " + textBox1.Text + "";
SqlCommand com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
var input = reader["subjects"].ToString();
var split = input.Split(new string { ",", " " }, StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j < split.Length; j++)
{
Label l = new Label();
l.Name = "label" + i.ToString();
l.Text = split[j];
l.Location = new Point(232, 100);
this.Controls.Add(l);
}
}
}
void addlabel1(int i1)
{
string cons = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
SqlConnection con = new SqlConnection(cons);
con.Open();
string str = "SELECT * FROM marks WHERE idno = " + textBox1.Text + "";
SqlCommand com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
var input1 = reader["smarks"].ToString();
var split1 = input1.Split(new string { ",", " " }, StringSplitOptions.RemoveEmptyEntries);
for (int z = 0; z < split1.Length; z++)
{
Label l1 = new Label();
l1.Name = "label" + i1.ToString();
l1.Text = split1[z];
l1.Location = new Point(353, 100);
this.Controls.Add(l1);
}
}
}
Any suggestions?
@Lucifer Does it matter, aren't multiple accounts allowed? meta.stackexchange.com/questions/35593/…
– Freggar
Jun 29 at 8:59
@Freggar I didn't knew that
– Lucifer
Jun 29 at 8:59
BTW, all the labels you are creating are in the same location, so they are one on top of the other, and you only can see the last one
– Pikoh
Jun 29 at 9:02
1 Answer
1
You, probably, want something like this (printing out the values as a table):
int left = 232;
int top = 100;
int step_x = 80;
int step_y = 20;
// outer loop - lines
while (reader.Read()) {
var split = Convert.ToString(reader["subjects"])
.Split(new string { ",", " " }, StringSplitOptions.RemoveEmptyEntries);
// inner loop - columns
for (int j = 0; j < split.Length; j++) {
new Label() {
Name = $"label{i}",
Text = split[j],
// Notice "top + step_y * j" - label after label
Location = new Point(left, top + step_y * j),
Parent = this, // equal to this.Controls.Add(l);
};
}
left += step_x;
}
only first record is displayed in all 5 labels out of 5 records in array......
– Ashwini Nemade
Jun 29 at 10:29
Please, check your query:
WHERE idno = " + textBox1.Text + ""
means that you filter out one record only.– Dmitry Bychenko
Jun 29 at 10:34
WHERE idno = " + textBox1.Text + ""
Yes... on dat one id there is column name-"subjects" where total 5 subjects are stored.... I am fetching that as an array and want to display each subject on each label by spliting that array.... but the output is displaying only one subject on each label
– Ashwini Nemade
Jun 29 at 10:36
Sorry, it seems to be a typo :
Location = new Point(left, top + step_y * j)
- * j
, not * i
– Dmitry Bychenko
Jun 29 at 10:40
Location = new Point(left, top + step_y * j)
* j
* i
thanks the output is correct now.... it helped!!
– Ashwini Nemade
Jun 29 at 10:58
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You cant call all your labels the same thing "l1.Name = "label" + i1.ToString();"
– BugFinder
Jun 29 at 8:53