I'm trying to make a form to update product prices
the form first fetch data from the database upon user selection of combobox and put it in text boxes
but for some reason, the update query on button click is not working, the product price doesn't get updated, and also doesn't give any errors
here is the code
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
productsearch();
}
public void productsearch()
{
string query = @"select ID, Productprice From Product where Productname LIKE @name";
using (var conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data source=|DataDirectory|\\crepeDB.accdb;"))
using (var cmd = new System.Data.OleDb.OleDbCommand(query, conn))
{
cmd.Parameters.Add("@name", System.Data.OleDb.OleDbType.VarChar).Value = comboBox1.Text;
conn.Open();
using (var rdr = cmd.ExecuteReader())
{
if (rdr.Read())
{
textBox2.Text = rdr["ID"].ToString();
textBox1.Text = rdr["Productprice"].ToString();
}
rdr.Close();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
string query = @"update Product set Productprice = @price where ID = @id";
using (var conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data source=|DataDirectory|\\crepeDB.accdb;"))
using (var cmd = new System.Data.OleDb.OleDbCommand(query, conn))
{
cmd.Parameters.Add("@id", System.Data.OleDb.OleDbType.VarChar).Value = textBox2.Text;
cmd.Parameters.Add("@price", System.Data.OleDb.OleDbType.Currency).Value = textBox3.Text;
conn.Open();
cmd.ExecuteNonQuery();
}
}
}