Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
    protected void Page_Load(object sender, EventArgs e)
    {
 
        {
            string aa = "";
            SqlConnection con = new SqlConnection(connStr);
 
            SqlCommand sd = new SqlCommand("select max(v)  as a from seats", con);
            con.Open();
            SqlDataReader drr = sd.ExecuteReader();
            if (drr.Read())
            {
                aa = drr["a"].ToString();
            }
            con.Close();
            int d = 0;
            int f = 0;
            f = Convert.ToInt16(aa);
            Label1.Text = f.ToString();
 

            string[] arr3 = new string[f];
            string[] arr5 = new string[f];
           // string[] arr6 = new string[f];

            SqlCommand cmd = new SqlCommand("select Table_No,Chair_total,status,v from seats", con);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                arr3[d] = dr["Table_No"].ToString();
                arr5[d] = dr["Chair_total"].ToString();
               // arr6[d] = dr["status"].ToString();
                d++;
            }
            con.Close();
 
            d = 0;
            for (int i = 1; i <= f; i++)
            {
                Button btnSubmit = new Button();
                btnSubmit.ID = i.ToString();
               // btnSubmit.Text = "Tabel_no :- " + arr3[d] + "\n" + "Chair_total:- " + arr5[d] + "\n" + "status:- " + arr6[d];
                btnSubmit.Text = "Tabel_no :- " + arr3[d] + "\n" + "Chair_total:- " + arr5[d] ;
                btnSubmit.Width = 120;
                btnSubmit.Height = 120;
                // btnSubmit.Click += new System.EventHandler(btnSubmit_click);

 
                d++;
                Panel1.Controls.Add(btnSubmit);
                Response.Write("\n");
 

 
                string c;
                SqlCommand cmd2 = new SqlCommand("select image from seats where Table_No='" + Tabel_no.Text + "' and Chair_total='" + Chait_total.Text + "' and status='" + status.Text + "' ", con);
                con.Open();
                SqlDataReader dr2 = cmd2.ExecuteReader();
                if (dr2.Read())
                {
                    c = dr2[0].ToString();
                    if (c == "red")
                        btnSubmit.BackColor = System.Drawing.Color.Red;
                    else if (c == "yellow")
                        btnSubmit.BackColor = System.Drawing.Color.Yellow;
                    else
                        btnSubmit.BackColor = System.Drawing.Color.Green;
                    c = "";
 
                }
                con.Close();
            }
        }
    }
 

 
i am creating the button dynamical using the database, my buttons are created but iam also trying to change the color of button using the value in database, and also trying to fetch some details from database to my button . but iam failed to do this, so plz check where iam wrong.
 

 
1- i want change the color of button using the database value.
2- want to print the details over the button text using the database value.
Posted 4-Nov-12 17:59pm
Comments
Sergey Alexandrovich Kryukov at 5-Nov-12 0:05am
   
Not a question. The problem is not explained. "I am failed" is not informative. What did you expect, what did you observe instead, why do you think it's wrong? Did you execute it under debugger?
--SA
Ravimcts at 5-Nov-12 0:41am
   
yes i debug lots of time, but main part is iam failed to retrieve the color database on button color, and i solve all the problems but failed to do this only, if u tell me another method to how to change the color of button using the database value .

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 == &quot;red&quot;)
                        ((Button)this.Control.FindByID["btn1"]).BackColor = System.Drawing.Color.Red;
                    else if (c == &quot;yellow&quot;)
                        ((Button)this.Control.FindByID["btn2"]).BackColor = System.Drawing.Color.Yellow;
                    else
                        ((Button)this.Control.FindByID["btn3"]).BackColor = System.Drawing.Color.Green;
                    c = "";
 
                }
con.Close();
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 209
1 Sergey Alexandrovich Kryukov 201
2 Nirav Prabtani 198
3 Mika Wendelius 170
4 OriginalGriff 167
0 Nirav Prabtani 398
1 OriginalGriff 292
2 Sergey Alexandrovich Kryukov 251
3 Mika Wendelius 185
4 Abhinav S 180


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100