locked
Error in C# Code RRS feed

  • Question

  • User-1251655565 posted

    I got error : Input string was not in a correct format for the code below

    lblAc.text = 92.53

    decimal CC = Convert.ToDecimal(lblAc.Text);
                if (CC >=90 && CC <= 100)
                {
                    lblrank.Text = "Excellent";
                }

    Any help, Thanks.

    Tuesday, June 28, 2016 6:31 PM

Answers

  • User281315223 posted

    As your error states, it sounds like the value that was present within your lblAc Control couldn't properly be parsed to a decimal. Are you absolutely sure that it contains the value "92.53"? You code works just fine if "92.53" is used as seen here.

    You may want to consider placing a breakpoint within your code to take a look at what lblAc contains. Are you sure you don't mean to be targeting a TextBox instead of a Label (i.e. "txtAc" instead of "lblAc"?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 28, 2016 6:40 PM

All replies

  • User281315223 posted

    As your error states, it sounds like the value that was present within your lblAc Control couldn't properly be parsed to a decimal. Are you absolutely sure that it contains the value "92.53"? You code works just fine if "92.53" is used as seen here.

    You may want to consider placing a breakpoint within your code to take a look at what lblAc contains. Are you sure you don't mean to be targeting a TextBox instead of a Label (i.e. "txtAc" instead of "lblAc"?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 28, 2016 6:40 PM
  • User61956409 posted

    Hi John,

    error : Input string was not in a correct format for the code below

    decimal CC = Convert.ToDecimal(lblAc.Text);

    As Rion said, please debug the code and check if you pass a specified string representation of a number to Convert.ToDecimal() method. Besides, if you prefer not to handle an exception if the conversion fails, you can call the Decimal.TryParse method instead.

    Best Regards,

    Fei Han

    Wednesday, June 29, 2016 6:57 AM