Every time I execute a DELETE query on my database, the following error results:
No value given for one or more required parameters
I check the names but still have the error. Below is the code used to execute the query:
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "delete FROM Accounts WHERE Id_No = " + IdNoBox.Text + "";
command.CommandText = query;
command.ExecuteNonQuery();
MessageBox.Show("Successfully Deleted");
this.Close();
connection.Close();
Assuming Id_No to be string, it should be enclosed in single quotes.Otherwise, it will be considered as a parameter. the query should string query = "delete FROM Accounts WHERE Id_No = "'" + IdNoBox.Text + "'";
To address the specific question being asked, if Id_No is a character based, there should be single quotes around it. For readability, consider the following syntax.
string query = string.Format("delete FROM Accounts WHERE Id_No = '{0}' ", IdNoBox.Text);
Also note that the connection/command should be disposed of properly, including the cases where an exception occurs. An easy way to do this is with the using clause. See below.
using (var connection = new OleDbConnection())
using (var command = new OleDbCommand(){Connection = connection,CommandText = query})
{
connection.Open();
command.ExecuteNonQuery();
}
MessageBox.Show("Successfully Deleted");
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.