8

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: Input string was not in a 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 strange combinations. Now I'm sure that issue is with strings and parsing them to decimal. When I created String variable - there was that same error while parsing. So can someone tell me what to do to be able to convert String to decimal?

6
  • 2
    TextBox.value is wrong. YourTextBoxID.Text is correct. Commented Apr 17, 2014 at 10:24
  • Did you try debugging? Commented Apr 17, 2014 at 10:27
  • Nope, TextBox is my id since im using html controlls. Commented Apr 17, 2014 at 10:27
  • Jakob Christensen - yes, and I cant see anything strange. When app is on line decimal newAmount ... it throws error beacuse string is in wrong format but I don't know why. Commented Apr 17, 2014 at 10:30
  • What does the string for item.Value look like on the decimal newAmount = line? Commented Apr 17, 2014 at 10:36

4 Answers 4

7

Try using "0,85" instead of "0.85". I think you can use the dot decimal if you change culture. Here is some more information: How to change symbol for decimal point in double.ToString()?

Sign up to request clarification or add additional context in comments.

Comments

2

Please try with the below code snippet.

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

3 Comments

The problem is not with TextBox - Im using html controls so TextBox as ID works just fine. The problem is in Decimal.Parse(SomeString).
please provide your textbox's value.
I don't use textbox. On this page I was using <input type="text" id="TextBox" runat="server" />. Anyway - Sn1ckers help me with the issue - problem was in my culture sign. Instead of 0.85 i should use 0,85.
0

Use TextBox.Text instead:

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

Comments

0

TextBox.value is wrong. YourTextBox.Text is correct...!

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

1 Comment

Same answer is already provided by '@David Brossard' before 3 minutes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.