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 isApple
and inbutton.Click
inremove
functioninform
isApple
;Loop 2
inform isBook
and inbutton.Click
inremove
functioninform
isBook
;Loop 3
inform isMarshmallow
and inbutton.Click
inremove
functioninform
isMarshmallow
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?