0

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

        }
    }
}
Joey Arnanzo
  • 329
  • 3
  • 18
  • If you mean the product price that is displayed in TextBox1, that won't automagically get updated just because you change it in the database. You would either have to bind that to a datasource tied to the query or manually update it upon success of the database update. To test this, run your code and make a change, then select that product again and see if Textbox1 now holds the new price. – Charles May Feb 25 '17 at 18:05
  • 1
    Same problem as before: Your `Parameters.Add` statements are in the wrong order. – Gord Thompson Feb 25 '17 at 18:22
  • @GordThompson if I'm getting this straight, you mean I should write the parameters.Add in the same order as the query? – Joey Arnanzo Feb 25 '17 at 18:27
  • Yes, as explained in the answer [here](http://stackoverflow.com/a/32008763/2144390). – Gord Thompson Feb 25 '17 at 19:00
  • Thank you, I didn't look at it thinking VB.net would be very different than C#, but thanks for your guidance – Joey Arnanzo Feb 25 '17 at 19:15

0 Answers0