1

I am working on winforms application and trying to save data to access DB on click on a button. I am using update method ( this is not the first time I am using this), but for some reason when I am saving the data, the values save in the DB are saved wrong. Can someone help me point out what I am doing wrong or if there is error ?

     string cb = "UPDATE Config SET features = @features, pricecost = @pricecost, price1 = @price1, tax = @tax, margem = @margem, price = @price, barcode = @barcode WHERE Code = @code";
        cmd = new OleDbCommand(cb, con);
        cmd.Parameters.Add("@features", OleDbType.VarChar).Value = txtFeatures.Text;
        cmd.Parameters.Add("@price", OleDbType.VarChar).Value = txt_Price.Text;
        cmd.Parameters.Add("@price1", OleDbType.VarChar).Value = txt_pricewithouttax.Text;
        cmd.Parameters.Add("@margem", OleDbType.VarChar).Value = txt_margem.Text;
        cmd.Parameters.Add("@pricecost", OleDbType.VarChar).Value = txt_pricecost.Text;
        cmd.Parameters.Add("@tax", OleDbType.VarChar).Value = txt_tax.Text;
        cmd.Parameters.Add("@barcode", OleDbType.VarChar).Value = txtBarcode.Text;
        cmd.Parameters.Add("@code", OleDbType.VarChar).Value = txtCode.Text;

For example, the txt_Price.Text save in @price1 and txt_margem.Text save in @price...

What can i do to solve this?

Thanks

Hussain Patel
  • 460
  • 3
  • 10
  • 24
d3pod
  • 86
  • 10
  • Walk through the code and make sure that the values you think go with the fields match up. It's likely a naming mismatch somewhere. – gilliduck Aug 21 '17 at 20:22
  • 1
    sometimes update statement should be in the same column order as the fields that you are updating are laid out.. can you also update your question and show us the database column schema of the Config table.. – MethodMan Aug 21 '17 at 20:22
  • 2
    I'm not sure the OLE DB driver for Access supports named parameters properly. Add parameters in order of their appearance in the query. Or apply a helper with a workaround, like featured [in this answer](https://stackoverflow.com/a/21925885/4137916). – Jeroen Mostert Aug 21 '17 at 20:25
  • 1
    Thanks for rapid help... @MethodMan you are right... the problem is the order. – d3pod Aug 21 '17 at 20:35

1 Answers1

1

You are using an OleDB connection to Access, the so called "Microsoft Jet". It is famous for not matching parameter names at all, and going by ordinal instead.

You need to add the parameters exactly in the same order as they appear in the command text, and you cannot use the same parameter twice as would be possible for example in SQL Server.

Watch your data types, storing prices as varchar is asking for trouble.

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
  • It´s the first time that i use Access database. I worked in projects with sql and mysql and don't had this problems. Thanks fo help – d3pod Aug 22 '17 at 09:00
  • yeah its a pain. brace for impact when you start using transactions in concurrent environments with access. in this case a real RDBMS surely is the way to go. – Cee McSharpface Aug 22 '17 at 09:32