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 ChkedRow = new List();
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!");
Mehmet FatihPosted Jun 28, 2023, 10:50 PM
I have solved porblem using the dataGridView1.Rows[j].Cells[1].Value instead of dataGridView1.Rows[j].Cells["gid"].Value
Thanks for your intrest.
Prasad RaveendranPosted Jun 28, 2023, 1:07 AM
Yes. that was I mentioned. Please review thorougly this column. The same column you are trying to use for deleting a record.
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["gid"].Value) == true) . You are converting the column dataGridView1.Rows[i].Cells["gid"].Value to boolean and the same column you have used in delete query which I got confused.
Mehmet FatihPosted Jun 27, 2023, 6:08 AM
Thanks for your intrest Prasad but the result is same. I am getting this error in this line.
Prasad RaveendranPosted Jun 27, 2023, 2:56 AM
Please cross check whether the key value which you are sending to script is valid.
Prasad RaveendranPosted Jun 27, 2023, 2:49 AM
check the below code