Hi,
after spending lot of time on google i am fadeup
i have a dynamic tab control in which i am able to make dynamic buttons and i am also able to get whole row of a table product in button's tag but i do not know how to retrive values from it i am not fimilar with tags
basicaly i have 8 fields in Tbl_Products now want to send name, price and quantity(no. of times button clicked)of the product to a list box on button cilck event here's my code

private void AddProductsToTab()
        {
            int i = 1;
            foreach (TabPage tp in tabControl1.TabPages)
            {
                FlowLayoutPanel flp = new FlowLayoutPanel();
                flp.Dock = DockStyle.Fill;
                SqlDataReader reader = null;
                SqlConnection con = new SqlConnection(ConStr);
                try
                {
                    string query = "SELECT * FROM Tbl_Products WHERE PTID = " + i.ToString();
                    con.Open();
                    SqlCommand com = new SqlCommand(query, con);
                    reader = com.ExecuteReader();
                    object[] productDetalis = new object[8];
                    while (reader.Read())
                    {
                        Button b = new Button();
                        b.Size = new Size(75, 75);
                        b.Text = reader.GetString(2);
                        b.Tag = reader.GetValues(productDetalis);
                        b.Click += new EventHandler(UpdateProductList);
                        flp.Controls.Add(b);
                    }
                    tp.Controls.Add(flp);
                    i++;
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    string msg = "Error:";
                    msg += ex.Message;
                    throw new Exception(msg);
                }
                finally
                {
                    con.Close();
                }
            }
        }

        void UpdateProductList(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            // what should i do??????
        }

or if someone can help me to convert the follwing code it is made up by using entites but i want to use sql

 private void AddProductsToTab()
        {
            int i = 1;

            foreach (TabPage tp in tabControl1.TabPages)
            {
                ObjectQuery<TblProduct> filteredProduct = new ObjectQuery<TblProduct>("SELECT VALUE P FROM TblProducts AS P WHERE P.ProductType = " + i.ToString(), posde);

                FlowLayoutPanel flp = new FlowLayoutPanel();

                flp.Dock = DockStyle.Fill;

                foreach (TblProduct tprod in filteredProduct)
                {
                    Button b = new Button();
                    b.Size = new Size(100, 100);
                    b.Text = tprod.Description;
                    b.Tag = tprod;
                    b.Click += new EventHandler(UpdateProductList);
                    flp.Controls.Add(b);
                }
                tp.Controls.Add(flp);
                i++;
            }

        }

        void UpdateProductList(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            TblProduct p = (TblProduct)b.Tag;
            products.Add(p);
            UpdateCustomerInfo(p);
            TransactionTotal = TransactionTotal + (decimal)p.Price;
            lbProductChoosen.SelectedIndex = lbProductChoosen.Items.Count - 1;
        }

        private void UpdateCustomerInfo(TblProduct product)
        {
            string CurrentDescription = product.Description;
            string Currentprice = String.Format("{0:c}", product.Price);
            string CurrentDescriptionPadded = CurrentDescription.PadRight(15);
            txtInfoPanel.Text = CurrentDescriptionPadded + Currentprice;
        }

The Tag property is an object. Basically cast it to whatever type of object you've set it too, the same as you're doing to cast sender as a button. For instance since you're setting the Tag property to an array of objects. So first you have to cast the tag as an array of objects, then cast each element of the type it's supposed to be and get the value freom it.

Here's an example of the different steps:

        //Object array of different field
        object[] productDetalis = new object[4]
            {
                "123",
                1234,
                "abcd",
                56789
            };

        //Set the Tag property with it
        button1.Tag = productDetalis;

        //Cast the array to a new array
        object[] newobjects = (object[])button1.Tag;

        /Assign each field to a variable
        string field1 = (string)newobjects[0];
        int field2 = (int)newobjects[1];
        string field3 = (string)newobjects[2];
        int field4 = (int)newobjects[3];

i did it but still getting error
Unable to cast object of type 'System.Int32' to type 'System.Object[]'.
on line number 4

void UpdateProductList(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            object[] pobjects = (object[])b.Tag;
            lbProductChoosen.Items.Add((string)pobjects[6]);

        }

Not sure where but the tag object seems to have been cast to int32, try using int32[] pobjects = (int32[])b.Tag;.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.