Fuhans Puji Saputra
Fuhans Puji Saputra

Reputation: 25

Delete a record successful, but it delete all records in the database in windows forms

i have a problem. This is happen when i tried to delete a single record in the database when the program runs. The delete record is working, but it is delete all record in the database. How do i fix that? First of all, i view data in my database using datagridview, and once the user click certain row, and the user click delete, it is supposed to delete a certain row that user has been selected, but it is delete all records.

Here is the code:

private void DeleteRecord(object sender, EventArgs e)
{
    if (fifthForm.comboBox1.Text == "English")
    {
        using (OleDbConnection conn = new OleDbConnection(connectionString))
        {
            string query = "DELETE FROM [SeranneRecord]";
            conn.Open();

            using (OleDbCommand cmd = new OleDbCommand(query, conn))
            {
                using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
                {
                    DataTable ds = new DataTable();
                    dataGridView.Rows.RemoveAt(dataGridView.CurrentRow.Index);
                    adapter.Update(ds);
                    dataGridView.DataSource = ds;
                    cmd.ExecuteNonQuery();

                    System.Media.SoundPlayer sound = 
                             new System.Media.SoundPlayer(
                             @"C:\Windows\Media\Windows Notify.wav");
                    sound.Play();
                    MessageBox.Show("Deleted Successfully", "Deleted");
                }
            }
        }
    }

Thanks, also i want to ask about how to modify the records using datagridview. Thanks

Upvotes: 0

Views: 607

Answers (1)

Нет войне
Нет войне

Reputation: 1139

You are removing the data grid row : (dataGridView.Rows.RemoveAt(dataGridView.CurrentRow.Index);) but you are not using any information from that row to create a WHERE clause in your query. You need something like `DELETE FROM tablename WHERE RecordName = 'RecordToDelete'.

Upvotes: 2

Related Questions