C# – Create a loop [closed]


I would like to create a loop in visual studio that dynamically creates a series of labels in a form.

private void Form1_Load(object sender, EventArgs e)
{
    getData();
}

public void getData()
{
    string conString = "server=xxx;uid=xxx;pwd=xxx;database=xxx";
    MySqlConnection con = new MySqlConnection(conString);
    con.Open();
    string query = "Select * from number where number > 100";
    MySqlCommand cmd = new MySqlCommand(query, con);
    MySqlDataReader reader = cmd.ExecuteReader();

    int y = 350;

    while (reader.Read())
    {   
       int numero = reader.GetInt32("number");

       Label label = new Label();
       label.Text = numero.ToString();
       label.Location = new Point(456, y);
       Controls.Add(label);

       y += 10;
     }
}

But it only creates one. Where am I going wrong? Thank you very much.

Leave a Reply

Your email address will not be published. Required fields are marked *