I am trying to run two parallel foreach
Loop
Code:
foreach (Control c in panel1.Controls)
{
if (c.GetType() == typeof(CheckBox))
{
if (((CheckBox)c).Checked)
{
id = name;
}
}
}
if (id != "")
{
foreach (Control cd in panel1.Controls)
{
if (cd.GetType() == typeof(TextBox) && cd.Name == name)
{
val = cd.Text.ToString();
if (val != "")
{
con3.Open();
SqlCommand cmd3 = new SqlCommand("insert into Employee_Ear_Ded values('" + Convert.ToInt32(name) + "','" + Convert.ToInt32(comboBox1.Text) + "','" + drpPayHead.Text + "','" + Convert.ToDouble(val) + "','" + comboBox2.Text + "')", con3);
cmd3.ExecuteNonQuery();
con3.Close();
}
else
{
MessageBox.Show("Please Enter Value");
}
}
}
}
else
{
MessageBox.Show("No Employee Selected");
}
Here I am trying to insert the value of the text box whose respective checkbox is Checked.
The code I am using is inserting only the last value checked.
How can I run it for each checkbox and its respective textbox?
The controls are created at runtime.