Reputation: 5
I am finding error of No value given for one or required parameter C# update query where ever i try to update my c# access data base here is the code... hope somebody will be of my assistant..
private void updatebutton_Click(object sender, EventArgs e)
{
try
{
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
string query = "update Emplyeedata set [ID]='" + midbox.Text + "',[Name]='" + mnamebox.Text + "',[Deisgnation]='" + mdesbox.Text + "',[Leave]='" + mleavebox.Text + "'Where [Name]='"+mnamebox.Text+"'";
cmd.CommandText = query;
cmd.ExecuteNonQuery();
con.Close();
cb();
MessageBox.Show("Updated", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Upvotes: 0
Views: 480
Reputation: 349
as a bit more improvement you can try like this, and if you can post the full error information.
private void updatebutton_Click()
{
using (OleDbConnection con = new OleDbConnection("Define your Connection String here"))
{
string query = @"UPDATE Emplyeedata
SET [id] = @ID
,[Name] = @Name
,[Deisgnation] = @Deisgnation
,[Leave] = @Leave
WHERE [Name] = @Name";
using (OleDbCommand cmd = new OleDbCommand(query, con) { CommandType = CommandType.Text })
{
cmd.Parameters.AddWithValue("@ID", midbox.Text);
cmd.Parameters.AddWithValue("@Name", mnamebox.Text);
cmd.Parameters.AddWithValue("@Deisgnation", mdesbox.Text);
cmd.Parameters.AddWithValue("@Leave", mleavebox.Text);
cmd.Parameters.AddWithValue("@Name", mnamebox.Text);
con.Open();
cmd.ExecuteNonQuery();
}
con.Close();
}
}
(this is a really unstructured way and please follow the SOLID Principle/s)
Upvotes: 1