System.FormatException: the input string was not in the correct format when converting the string to decimal.

I have a little problem with ASP.NET and C #. This is my error code:

An exception of type "System.FormatException" occurred in mscorlib.dll, but was not handled in> user code

Additional information: the input line was not in the correct format.

protected void Page_Load(object sender, EventArgs e)
{
    if(this.IsPostBack == false)
    {
        Currency.Items.Add(new ListItem("Euro", "0.85"));
        Currency.Items.Add(new ListItem("Yen", "11.30"));
        Currency.Items.Add(new ListItem("PLN", "4.20"));
        Currency.Items.Add(new ListItem("GBP", "5.62"));
    }
}

protected void Convert_Click(object sender, EventArgs e)
{
    decimal oldAmount;
    bool succes = Decimal.TryParse(TextBox.Value, out oldAmount);

    if(succes)
    {
        ListItem item = Currency.Items[Currency.SelectedIndex];
        decimal newAmount = oldAmount * decimal.Parse(item.Value);
        Result.InnerText = "Result: " + newAmount;
    }

}

I tried Decimal.Parse, Decimal.TryParse and other weird combinations. Now I am sure that the problem is with strings and parsing them into decimal. When I created the String variable, there was the same error during parsing. So can someone tell me what to do to convert String to decimal?

+4
4

"0,85" "0,85". , , . : double.ToString()?

+3

.

CultureInfo info = CultureInfo.GetCultureInfo("es-ES");
string storedValue = "3,85";
decimal oldAmount;
bool succes = Decimal.TryParse(storedValue, NumberStyles.Any, info, out oldAmount);
+1

The value of TextBox.value is incorrect. YourTextBox.Text is correct ...!

bool success = Decimal.TryParse(TextBox.Text, out oldAmount);
+1
source

Use TextBox.Text instead:

bool succes = Decimal.TryParse(TextBox.Text, out oldAmount);
0
source

Source: https://habr.com/ru/post/1536930/


All Articles