0

I wrote a tiny software that retrieves some data from a MySQL database and shows them through labels in a table format. Now for each "row" (or information) the user has the possibility to remove that info from the MySQL Database. So, that's some piece of code:

public Form1()
{
   InitializeComponent();
   MySqlConnection conn = null;
   MySqlDataReader mainrdr = null;
   if (open_connection()) //open_connection simply opens a connection to MySQL DB and returns true or false according if the connection was opened with success or something else.
   {
      string query = "SELECT * FROM res_ev;";
      MySqlCommand cmd = new MySqlCommand(query, conn);
      mainrdr = cmd.ExecuteReader();
      While(mainrdr.Read())
      {
        Label label = new Label();
        Button button = new Button();
        ...
        ... //DEFINING PROPERTY OF THESE OBJECTS
        ...
        string inform = mainrdr.GetString(0);
        button.Click += new EventHandler((s, e) => remove(s, e, inform));
        ...
        ...
      }
      ...
   }
}
private void remove(object sender, EventArgs e, string info)
{
   MessageBox.Show(info);
   ...
   ...
}

The problem is:
The string "inform" during the While clause assumes Always the right information

For ex.

  • Loop 1
    inform is Apple
    and in button.Click in remove function inform is Apple;

  • Loop 2
    inform is Book
    and in button.Click in remove function inform is Book;

  • Loop 3
    inform is Marshmallow
    and in button.Click in remove function inform is Marshmallow

but when I click each button dynamically created, all shows up the last string that was assumed by inform during the While clause.

So I want that if I click the first button (created in loop 1) it has to show "Apple", the second one "Book" and the last one "Marshmallow".

Any idea to resolve that?

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Ricks23
  • 11
  • 1
  • 2
  • can you explain please what you mean `but when I click each button **dynamically** created` ? – AsfK Sep 06 '15 at 12:36
  • What you describe is called a closure. Your code sets it up correctly. If it doesn't work, then what you show is not in fact your actual code. – GSerg Sep 06 '15 at 12:39
  • AsfK, for example, if the While loop repeats itself for 10 times I'll have 10 labels and 10 buttons created in that While by the code itself. So, I need that for each button, It shows a different value of information. I used the dynamically term because all these buttons and labels are generated according the repeats of the loop. GSerg the code has been cleaned by unnecessary (tested) instructions and simplified so as to facilitate the understanding of the code itself, but this is the "heart" of my software. – Ricks23 Sep 06 '15 at 12:58
  • 2
    Is your code sample correct? Isn't the `inform` variable declared outside of the while loop? – jeroenh Sep 06 '15 at 13:07
  • @Ricks23 You might have cleaned too much. The code from your question works and shows different `inform` for each button. Which is because `inform` is declared inside the loop. – GSerg Sep 06 '15 at 13:10
  • Mate, you solved my issue! – Ricks23 Sep 06 '15 at 13:10

2 Answers2

0

It seems you add the same button to the form.

How do you add the buttons to the form?

  • 1
    more a comment then an answer - please integreate into the `SO` community, get your 15 reputation and then add comments. Don't add such "idonthaveenoughreptocomment" answers!! – cramopy Sep 06 '15 at 12:54
0

I solved my issue!. The problem was this: In my actual code I had

public Form1()
{
string inform;
InitializeComponent();
MySqlConnection conn = null;
MySqlDataReader mainrdr = null;
if (open_connection())   {
  string query = "SELECT * FROM res_ev;";
  MySqlCommand cmd = new MySqlCommand(query, conn);
  mainrdr = cmd.ExecuteReader();
  While(mainrdr.Read())
  {
    Label label = new Label();
    Button button = new Button();
    ...
    ... //DEFINING PROPERTY OF THESE OBJECTS
    ...
    inform = mainrdr.GetString(0);
    button.Click += new EventHandler((s, e) => remove(s, e, inform));
    ...
    ...
  }
  ...
  }
}

While the code posted above is really correct.. so what I don't understand is why if I define string inform in the While it works good and if I define string inform outside the While loop it doesn't work?

Ricks23
  • 11
  • 1
  • 2