Foreach loop not reaching going further than first panel items
<p>It seems to me that my foreach loop is not reaching my second panel or something is making it skip the items in the loop.</p>

<p>Here is the creating of the panels and loop(I know the SQL is vulnerable to injections but for the sake of making the question shorter I just used SQL):</p>

<p>This is what is created for every product in the product table:</p>

<pre><code>Label lbName = new Label();
lbName.Text = name + "\n" + row[7].ToString();
lbName.Height = 40;
lbName.Width = 150;
lbName.Name = name + row[7].ToString();
lbName.Location = new Point(ptX, ptY);
pt.Controls.Add(lbName);

Label lbID = new Label();
lbID.Text = row[0].ToString();
lbID.Height = 40;
lbID.Width = 150;
lbID.Name = "ID" + name + row[7].ToString();
lbID.Location = new Point(ptX, ptY);
lbID.Visible = false;
pt.Controls.Add(lbID);

TextBox txtStockCount = new TextBox();
txtStockCount.Text = "0";
txtStockCount.Height = 40;
txtStockCount.Width = 100;
txtStockCount.Name = "txtTS" + name + row[7].ToString();
txtStockCount.Location = new Point(ptX + 150, ptY);
txtStockCount.GotFocus += txtStockCount_GotFocus;
txtStockCount.KeyPress += txtStockCount_KeyPress;
txtStockCount.LostFocus += txtStockCount_LostFocus;
pt.Controls.Add(txtStockCount);

TextBox txtBroken = new TextBox();
txtBroken.Text = "0";
txtBroken.Height = 40;
txtBroken.Width = 100;
txtBroken.Name = "txtTB" + name + row[7].ToString();
txtBroken.Location = new Point(ptX + 300, ptY);
txtBroken.GotFocus += txtStockCount_GotFocus;
txtBroken.KeyPress += txtStockCount_KeyPress;
txtBroken.LostFocus += txtStockCount_LostFocus;
pt.Controls.Add(txtBroken);

TextBox txtRecieve = new TextBox();
txtRecieve.Text = "0";
txtRecieve.Height = 40;
txtRecieve.Width = 100;
txtRecieve.Name = "txtTR" + name + row[7].ToString();
txtRecieve.Location = new Point(ptX + 450, ptY);
txtRecieve.GotFocus += txtStockCount_GotFocus;
txtRecieve.KeyPress += txtStockCount_KeyPress;
txtRecieve.LostFocus += txtStockCount_LostFocus;
pt.Controls.Add(txtRecieve);

Label lbDifference = new Label();
lbDifference.Text = " 0 ";
lbDifference.Height = 40;
lbDifference.Width = 150;
lbDifference.Name = "lblDiff" + name + row[7].ToString();
                    lbDifference.Location = new Point(ptX + 600, ptY);
pt.Controls.Add(lbDifference);

Button btnConfirm = new Button();
btnConfirm.Text = "Confirm";
btnConfirm.Height = 30;
btnConfirm.Width = 80;
btnConfirm.Name = "btnConfirm" + name + row[7].ToString();
                    btnConfirm.Location = new Point(ptX + 750, ptY);
                    btnConfirm.Click += btnConfirm_Click;
pt.Controls.Add(btnConfirm);
</code></pre>

<p>and the reading the panels to update the database from the text boxes:</p>

<pre><code>private void txtStockCount_LostFocus(object sender, EventArgs e)
{
        TextBox txtB = (sender as TextBox);
        string name = txtB.Name.Remove(0,5);
        string prodID = "";
        string QtyToday = "";
        string QtyBD = "";
        string QtyRev = "";
        string table = "";

        foreach (Panel p in pnls)
        {
            IEnumerable&lt;Label&gt; labels = p.Controls.OfType&lt;Label&gt;();
            foreach (Label label in labels)
            {
                string Compare = label.Name.Remove(0, 2);
                if (name == Compare)
                {
                    prodID = label.Text;
                    if (Regex.IsMatch(Compare, @"^[a-hA-H]"))
                    {
                        table = "ProductHistoryAH";
                    }
                    else if (Regex.IsMatch(Compare, @"^[i-qI-Q]"))
                    {
                        table = "ProductHistoryIQ";
                    }
                    else if (Regex.IsMatch(Compare, @"^[r-zR-Z]"))
                    {
                        table = "ProductHistoryRZ";
                    }
                }
            }

            string sql = "Select * From Product WHERE ProdID = '" + prodID + "'";
            DataTable dtP = db.GetDataTable(sql);

            IEnumerable&lt;TextBox&gt; textBoxes = p.Controls.OfType&lt;TextBox&gt;();
            foreach (TextBox textBox in textBoxes)
            {
                string name1 = textBox.Name.Remove(0, 5);
                label2.Text = name1;
                if (name == name1)
                {
                    if (textBox.Name == "txtTS" + dtP.Rows[0][1].ToString() + dtP.Rows[0][7].ToString())
                    {
                        QtyToday = textBox.Text;
                    }

                    if (textBox.Name == "txtTB" + dtP.Rows[0][1].ToString() + dtP.Rows[0][7].ToString())
                    {
                        QtyBD = textBox.Text;
                    }

                    if (textBox.Name == "txtTR" + dtP.Rows[0][1].ToString() + dtP.Rows[0][7].ToString())
                    {
                        QtyRev = textBox.Text;
                    }
                }
            }                

            string sql1 = "Select * From " + table + " WHERE ProdID = '" + prodID + "' AND ProdHDate = '" + DateTime.Today.Date.ToShortDateString() + "'";
            DataTable dt = db.GetDataTable(sql1);                

            if (dt.Rows.Count == 0)
            {
                dbh.addnewstockhistory(ph);

            }
            else if (dt.Rows.Count &gt; 0)
            {                    
                dbh.updatestockhistory(ph);
            }

        }            
}
</code></pre>

<p>Everything works 100%, it inserts and updates data with ease, with the first panel but no other textboxes on the other panels work.</p>

<p>The error lies in the foreach panel loop, the foreach loops inside the panel loop. It says there is a syntax error near the where of string sql1. But what i dont understand is that it works for the first panel but non of the other panels and it does have the prodID, I know this cause i placed the statement in a label just to see how it looks.</p>

<p>Please help. Ask for anything if you need.</p>