Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
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
Comments
Sergey Alexandrovich Kryukov 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 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

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

C#
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
C#
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();
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900