C#如果在面板中选中复选框,则继续

时间:2013-12-29 05:22:14

标签: c# checkbox foreach panel checked

如果我的面板中的复选框被选中,我正在尝试找到填充访问数据库的最简单方法。如果没有选中复选框,则不执行任何操作。以下是我所拥有的,但是当我选择多个复选框时,我收到错误:

  

更改会为索引创建重复值

foreach (Control c in panel4.Controls.Cast<Control>())
{
    if (c is CheckBox)
    {
        if (((CheckBox)c).Checked)
        {
            string constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\\test\\Comm.accdb";

            string cmdstr = "Insert into test(inc1,inc2,inc3,inc3,inc4,inc5)Values(@a,@b,@c,@d,@e,@f)";

            OleDbConnection con = new OleDbConnection(constr);
            OleDbCommand com = new OleDbCommand(cmdstr, con);
            con.Open();

            com.Parameters.AddWithValue("@a", label6.Text);
            com.Parameters.AddWithValue("@b", textBox1.Text);
            com.Parameters.AddWithValue("@c", comboBox3.Text);
            com.Parameters.AddWithValue("@d", checkBox17.CheckState);
            com.Parameters.AddWithValue("@e", checkBox16.CheckState);
            com.Parameters.AddWithValue("@f", checkBox18.CheckState);
            com.ExecuteNonQuery();
            con.Close();
        }
    }
}

0 个答案:

没有答案