简体   繁体   中英

No given value for one or more required parameters when deleting a row

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);}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM