Mehmet Fatih

Mehmet Fatih

  • 858
  • 939
  • 41.2k

Delete the selected datas from database with checboxes

Jun 26 2023 10:25 PM

I am trying to delete the selected datas from database with checkboxes. But it doesn't delete the datas. I couldn't understanf the reason. Can you have a look?

int i = 0;
List<int> ChkedRow = new List<int>();

for (i = 0; i <= dataGridView1.RowCount - 1; i++)
{
    if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["gid"].Value) == true)
    {
        ChkedRow.Add(i);
    }
}
if (ChkedRow.Count == 0)
{
    MessageBox.Show("En az bir checkbox seçiniz!");
    return;
}
foreach (int j in ChkedRow)
{
    try
    {
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }

        var cmdText = @"DELETE FROM gezilistemiz25 WHERE gid =" + dataGridView1.Rows[j].Cells["gid"].Value + "";

        var command = new OleDbCommand(cmdText, conn);
        command.ExecuteNonQuery();
        conn.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
MessageBox.Show("Kayitlar basariyla eklendi!");

Answers (5)