C#删除数据库中的所有复选框

C#删除数据库中的所有复选框,c#,sql-server,C#,Sql Server,我在删除数据库中的复选框时遇到问题。 它表明 字符串未被识别为有效的布尔值 在这一行: if (bool.Parse(item.Cells[0].Value.ToString())) 这是我检查所有复选框的代码 if (checkBox2.Checked == false) { foreach (DataGridViewRow row in dataGridView1.Rows) { DataGr

我在删除数据库中的复选框时遇到问题。 它表明

字符串未被识别为有效的布尔值

在这一行:

if (bool.Parse(item.Cells[0].Value.ToString()))
这是我检查所有复选框的代码

 if (checkBox2.Checked == false)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
                chk.Value = chk.TrueValue;

            }
        }
        else if (checkBox2.Checked == true)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
                chk.Value = 1;

                if (row.IsNewRow)
                {
                    chk.Value = 0;
                }
            }
        }
//这是我删除数据库复选框的代码

int count = 0;
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if ((Convert.ToBoolean(row.Cells[0].Value) == true))
            {
                count++;
            }
        }

        if (count == 0)
        {
            MessageBox.Show("Please select an item to delete");
        }
        else
        {

            foreach (DataGridViewRow item in dataGridView1.Rows)
            {
                if (bool.Parse(item.Cells[0].Value.ToString()))
                {

                    connection.Close();
                    connection.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = connection;

                    cmd.CommandText = "INSERT INTO tbl_Archive ([ID],[Date],[StartTime],[EndTime],[NameOfSchool],[Pax],[TourAgency],[Coordinator],[ContactNumber],[Date & Time Added]) VALUES ('"
                    + id + "','" + myDate + "','" + start + "','" + end + "','" + school + "','" + pax + "','" + touragency + "','" + coordinator + "','" + contact + "','" + dateadded + "')";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "DELETE FROM tbl_Schedule WHERE [ID] = '" + item.Cells[1].Value.ToString() + "'";
                    cmd.ExecuteNonQuery();
                    cmd.CommandText = "DELETE FROM tbl_ScheduledVisitors WHERE [ID] = '" + item.Cells[1].Value.ToString() + "'";
                    cmd.ExecuteNonQuery();
                    connection.Close();

                }

            }
            MessageBox.Show("Successfully Deleted");
            refreshgrid();
            checkBox2.Checked = false;

            //}
        }

为了诊断问题,您可以在以下两行设置断点:

if ((Convert.ToBoolean(row.Cells[0].Value) == true))

然后查看
item.Cells[0].value的值是多少


很可能它不是一个有效的布尔字符串

为了诊断问题,您可以在以下两行设置断点:

if ((Convert.ToBoolean(row.Cells[0].Value) == true))

然后查看
item.Cells[0].value的值是多少


很可能它不是一个有效的布尔字符串

您可以替换以下内容:

if (bool.Parse(item.Cells[0].Value.ToString()))
为此:

if (Convert.ToBoolean(item.Cells[0].Value))

您可以替换以下内容:

if (bool.Parse(item.Cells[0].Value.ToString()))
为此:

if (Convert.ToBoolean(item.Cells[0].Value))

SQL注入有人吗?SQL注入有人吗?我认为chk.TrueValue没有返回一个真正的布尔值,它只是检查复选框。我认为chk.TrueValue没有返回一个真正的布尔值,它只是检查复选框