2

I am trying to delete Data from my database via a button click however I am currently getting the error:

"An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: No value given for one or more required parameters."

This is my code for the delete button:

private void btnDelete_Click(object sender, EventArgs e)
    {
        connect.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\Foundation Degree\Year 2\Chris Ovia\Assignment2\Assignment2\BookSellersDatabase.mdb";
        tempTitle = txtTitle.Text;
        connect.Open();
        OleDbCommand cmd = new OleDbCommand("DELETE FROM Book WHERE Title = tempTitle", connect);
        cmd.ExecuteNonQuery();
        MessageBox.Show("Data Deleted", "Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);
        connect.Close();

        //this.Refresh();
        //Application.DoEvents(); 
    }

1 Answer 1

3

The delete query should be

OleDbCommand cmd = new OleDbCommand("DELETE FROM Book WHERE Title ='" +  tempTitle + "'", connect);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you that has worked :D While I'm here, do you know how to make it so that it refreshes the form after it has deleted the data?
You will need to clear each textbox then. If you however use binding as a mechanism to bind textboxes to data, you can clear the information with a single line of code.
Using string interpolation (for better visuals), OleDbCommand cmd = new OleDbCommand($"DELETE FROM Book WHERE Title ='"{ tempTitle }"'", connect);.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.