hi
see, In your code your control is "btnSubmit" as Button.
But when you dynamically place it on form it id was change as per your database field value (btnSubmit.ID = i.ToString();)
so now your button id like 1, 2, 3, 4.......
now your are adding this control on page.
and after it you are changing the color of "btnSubmit" which can not find on page.
so, you have to change color of 1,2,3,4.....
like
con.Open();
SqlDataReader dr2 = cmd2.ExecuteReader();
if (dr2.Read())
{
c = dr2[0].ToString();
if (c == "red")
((Button)this.Control.FindByID["1"]).BackColor = System.Drawing.Color.Red;
else if (c == "yellow")
((Button)this.Control.FindByID["2"]).BackColor = System.Drawing.Color.Yellow;
else
((Button)this.Control.FindByID["3"]).BackColor = System.Drawing.Color.Green;
c = "";
}
con.Close();
And remember that the only numeric name of any control will make trouble in coading.
so you have to generate it with prefix like "btn" + i.ToString()
so your control will be added as btn1, btn2, btn3, btn4........
and you can change color like
con.Open();
SqlDataReader dr2 = cmd2.ExecuteReader();
if (dr2.Read())
{
c = dr2[0].ToString();
if (c == "red")
((Button)this.Control.FindByID["btn1"]).BackColor = System.Drawing.Color.Red;
else if (c == "yellow")
((Button)this.Control.FindByID["btn2"]).BackColor = System.Drawing.Color.Yellow;
else
((Button)this.Control.FindByID["btn3"]).BackColor = System.Drawing.Color.Green;
c = "";
}
con.Close();