0

On LOAD my WPF is running this code, that should change the BackColor of the buttons that are listed in my Database.
So "reader1.GetString("seat")" have the Seat number, and i have created buttons with the same name as the seat. So i just want it to load the seat number from sql, and change the backcolor of that button with the name loaded.

example: "reader1.GetString("seat")" comes up with seat: W12, then my Button W12 should change BackColor.

I'm just getting the error cannot convert string to button.. i Have tried many things, but this will not work for me.

MySqlCommand cmd1 = new MySqlCommand("SELECT * FROM guests", _sqlhost);
            _sqlhost.Open();
            MySqlDataReader reader1 = cmd1.ExecuteReader();
            while (reader1.Read())
            {
                if (reader1.GetString("seat") != null)
                {
                    string btn = reader1.GetString("seat");
                    this.Button[btn].BackColor = Brushes.Red;
                }
            }
_sqlhost.Close();
Clemens
  • 123,504
  • 12
  • 155
  • 268
CaMeX
  • 89
  • 10
  • Possible duplicate of [How can I find WPF controls by name or type?](http://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type) – Pikoh Nov 04 '16 at 10:08

1 Answers1

2

Try using this code instead:

var myButton = (Button)this.FindName(btn);

Check this post for more info: How can I find WPF controls by name or type?

Community
  • 1
  • 1
Lupu Silviu
  • 1,145
  • 11
  • 23