0

Hi i want to update my ITEM_DETAILS table in C# using Sql server at the backend but when i am doing this i am getting the following error :

failed to convert parameter value from a string to a decimal. c#

Here is my code:

int x;

da.UpdateCommand = new SqlCommand("UPDATE ITEM_DETAILS SET ITEM_NAME=@ITEM_NAME,ITEM_DESCRIPTION=@ITEM_DESCRIPTION,VENDOR_NAME=@VENDOR_NAME,QUANTITY=@QUANTITY,RATE=@RATE,AMOUNT=@AMOUNT,INVOICE_NUM=@INVOICE_NUM,DATE=@DATE WHERE ITEM_MODEL=@ITEM_MODEL", con);
da.UpdateCommand.Parameters.Add("@ITEM_NAME", SqlDbType.VarChar).Value = txtItemName.Text;
da.UpdateCommand.Parameters.Add("@ITEM_DESCRIPTION", SqlDbType.VarChar).Value = txtItemDescription.Text;
da.UpdateCommand.Parameters.Add("@VENDOR_NAME", SqlDbType.VarChar).Value = txtVendor.Text;
da.UpdateCommand.Parameters.Add("@QUANTITY", SqlDbType.Int).Value = txtQuantity.Text;
da.UpdateCommand.Parameters.Add("@RATE", SqlDbType.Money).Value = txtRate.Text;
da.UpdateCommand.Parameters.Add("@AMOUNT", SqlDbType.Money).Value = txtAmount.Text;
da.UpdateCommand.Parameters.Add("@INVOICE_NUM", SqlDbType.Int).Value = txtInvoice.Text;
da.UpdateCommand.Parameters.Add("@DATE", SqlDbType.VarChar).Value = dateTimePicker1.Value;
da.UpdateCommand.Parameters.Add("@ITEM_MODEL", SqlDbType.VarChar).Value = ds.Tables[0].Rows[bs.Position][0];

con.Open();
x = da.UpdateCommand.ExecuteNonQuery();
con.Close();

if (x >= 1)
{
    MessageBox.Show("Record(s) has been updated");
}

Can anyone explain me the solution to this problem?

dcastro
  • 66,540
  • 21
  • 145
  • 155
Kretos
  • 91
  • 1
  • 3
  • 10
  • which field is in decimal? txtAmount? If yes, check that your separator is the 'point' like '1.23' and not '1,23'. Check also space or others characters – Portekoi Mar 01 '14 at 21:09
  • possible duplicate of [Failed to convert parameter value from a String to a Decimal?](http://stackoverflow.com/questions/12982208/failed-to-convert-parameter-value-from-a-string-to-a-decimal) – dcastro Mar 01 '14 at 21:11

1 Answers1

1
da.UpdateCommand.Parameters.Add("@RATE", SqlDbType.Money).Value = txtRate.Text;
da.UpdateCommand.Parameters.Add("@AMOUNT", SqlDbType.Money).Value = txtAmount.Text;

You need to parse the txtRate.Text and txtAmount.Text to Currency.

string.Format("{0:#.00}", Convert.ToDecimal(myMoneyString) / 100);

See Convert String to Money

In VB, we would use the CType() conversion.

Community
  • 1
  • 1
Mark C.
  • 6,332
  • 4
  • 35
  • 71