When I enter a number in the ChbBeds_numericUpDown
and click on the "Update" button, it says "Data Updated", but nothing changes in the database
private void ChbUp_button_Click(object sender, EventArgs e)
{
try
{
string statement = "UPDATE ChamberXPavilions SET Beds count = @beds_count WHERE Pav_name = @pav_name AND Chamber_number = @chamber_number";
cmd = new OleDbCommand(statement, conn);
cmd.Parameters.AddWithValue("@pav_name", Chbpav_comboBox.Text);
cmd.Parameters.AddWithValue("@chamber_number", Chb_numericUpDown.Value);
cmd.Parameters.AddWithValue("@beds_count", ChbBeds_numericUpDown.Value);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Data updated");
showdata();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Is the SQL statement wrong ?
CodePudding user response:
Contrary to SQL Server, the OleDB provider for MS Access does NOT work with named parameters - instead, it uses positional parameters.
In your case, you have a SQL statement
UPDATE ChamberXPavilions
SET Beds count = @beds_count
WHERE Pav_name = @pav_name AND Chamber_number = @chamber_number
so you need to also provide the parameters in the same order - first @beds_count
, then @pav_name
and finally @chamber_number
.
So try this for providing the parameter values:
cmd = new OleDbCommand(statement, conn);
cmd.Parameters.AddWithValue("@beds_count", ChbBeds_numericUpDown.Value);
cmd.Parameters.AddWithValue("@pav_name", Chbpav_comboBox.Text);
cmd.Parameters.AddWithValue("@chamber_number", Chb_numericUpDown.Value);
Now, your UPDATE
statement should get the proper values and should now work