0

I'm trying to delete a row from my database, When I click to a row then press then my delete button it says that "No given value for one or more required parameters", I don't whats the error, I checked my table name and it's correct. My other delete button works fine

DialogResult result = MessageBox.Show("Are you sure you want to delete this user?",
           "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (result == DialogResult.Yes)
        {
            string sql = string.Format(@"DELETE FROM User_list WHERE ID ={0}",
                dataGridView2.CurrentRow.Cells[0].Value.ToString());
            crudTools.ExecuteQuery(sql);
            crudTools.FillDataGrid("SELECT * FROM User_list", ref dataGridView2);}
Janelle
  • 5
  • 4

1 Answers1

0

The problem seems originated from this query string inside string.Format:

string sql = string.Format(@"DELETE FROM User_list WHERE ID ={0}", dataGridView2.CurrentRow.Cells[0].Value.ToString());

No given value for one or more required parameters indicates that the passed value in WHERE clause isn't valid, what you need to do if the ID is a string column just add single quotes around first parameter marked with {0}, because dataGridView2.CurrentRow.Cells[0].Value.ToString() returns a string but without any single quotes enclosure it will be treated by Access as numeric value:

string sql = string.Format(@"DELETE FROM User_list WHERE ID = '{0}'", dataGridView2.CurrentRow.Cells[0].Value.ToString());

Or if the ID is an integer column, convert it to int before passing:

string sql = string.Format(@"DELETE FROM User_list WHERE ID = {0}", Convert.ToInt32(dataGridView2.CurrentRow.Cells[0].Value.ToString()));

NB: Remember to use parameterized query if the value comes from user input inside DataGridView.

Similar issue:

No value given for one or more required parameters. error during Search

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • I already tried your both codes, but it string sql = string.Format(@"DELETE FROM User_list WHERE ID = '{0}'", dataGridView2.CurrentRow.Cells[0].Value.ToString()); and the Convert.ToInt32 code but the result is same – Janelle Oct 03 '17 at 10:57
  • What value returned by `dataGridView2.CurrentRow.Cells[0].Value`? The error states that you've missing one argument in `WHERE` clause and I think the potential query to break in your code is only that line, are you sure the execution flow breaks in `crudTools.ExecuteQuery(sql)`? – Tetsuya Yamamoto Oct 03 '17 at 13:38