locked
Error converting data type varchar to decimal RRS feed

  • Question

  • User-1499457942 posted

    Hi

      On below line i get above error . Value is blank/empty

    cmd.Parameters.AddWithValue("@Amount", SqlDbType.Decimal).Value = txt_Amount.Text;

    Thanks

    Wednesday, September 5, 2018 7:58 AM

Answers

  • User-168407370 posted

    If DB Datatype is decimal than you must have to convert varchar to deciamal.

    Try this type of convert it surely work for you

    var text = "15";
    var abc = Convert.ToDecimal(string.IsNullOrWhiteSpace(text) ? "0" : text);
    Console.WriteLine(abc);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 5, 2018 9:53 AM

All replies

  • User1120430333 posted

    JagjitSingh

    Hi

      On below line i get above error . Value is blank/empty

    cmd.Parameters.AddWithValue("@Amount", SqlDbType.Decimal).Value = txt_Amount.Text;

    Thanks

    For it to even work, the tbxbox.text data would have to be converted from numeric text data that's in a decimal format to true numeric decimal.

    A validation check should be made well before it even got to the point of even being used as a parameter  to determine if the text had valid data in it,  that is numeric and  in a decimal format.

    If the txtbox.text is string.empty, then you need to be making a decision on what to do if the txtbox.text is string.empty, like you force the user to enter 0,  or you put the 0 in the txtbox.text when the screen is shown and it can't  be left blank, because the conversion of the data from text numeric decimal number to a true numeric decimal value is going to blow up if the value is string.empty.

    Wednesday, September 5, 2018 9:31 AM
  • User-168407370 posted

    If DB Datatype is decimal than you must have to convert varchar to deciamal.

    Try this type of convert it surely work for you

    var text = "15";
    var abc = Convert.ToDecimal(string.IsNullOrWhiteSpace(text) ? "0" : text);
    Console.WriteLine(abc);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 5, 2018 9:53 AM