none
error in formatting string RRS feed

  • Question

  • Hi

    i have to subtract one text box value from other text box value and then show the result in the 3rd text box so how can id that i have some code but it give me string format error

    Input string was not in a correct format.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.FormatException: Input string was not in a correct format.

    Source Error: 
    Line 48:         int RegistrationResult;
    Line 49:         int TutionResult;
    Line 50:         RegistrationResult = Convert.ToInt32(TbRegistration.Text) - Convert.ToInt32(TbPaidFees.Text);
    Line 51:         TutionResult = Convert.ToInt32(TbTutionFees.Text) - Convert.ToInt32(TbPaidFees.Text);


    unseendreamzzz

    Thursday, May 3, 2012 7:46 PM

Answers

  • You certainly have a string that is not in a correct format.

    You can also try int.TryParse instead of Convert.ToInt32.  It returns a bool, indicating whether or not parsing was successful.

    The exception comes from trying to convert text that is not a number.

    Some examples of strings that DO convert to Int32:

    • "    10    "
    • "\t10\n"
    • "-10"
    • "10\u0000"

    Some examples of strings that DO NOT convert to Int32:

    • "11,000"
    • "29.95"
    • "10.0"
    • "1 234 567"
    • "$10"
    • "10 cm"
    • "2A6B3F"
    • "10\u00000"


    Thursday, May 3, 2012 8:32 PM
  • int number = 0; if (!int.TryParse(inputText.Text, out number))
    Console.WriteLine("invalid number);


    http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx

    MCP: ASP.NET
    Langauges: VB.net, C#, Java, C++ (non-CLI), ActionScript, JavaScript, (X)HTML, XAML, MXML

    • Proposed as answer by Papy Normand Thursday, May 3, 2012 10:22 PM
    • Marked as answer by unseendreamzzz Friday, May 4, 2012 1:31 PM
    Thursday, May 3, 2012 9:17 PM

All replies

  • What values did you put into each textbox?

    MCP: ASP.NET
    Langauges: VB.net, C#, Java, C++ (non-CLI), ActionScript, JavaScript, (X)HTML, XAML, MXML

    Thursday, May 3, 2012 8:06 PM
  • i is value e.g numbers like 1000.000

    unseendreamzzz

    Thursday, May 3, 2012 8:31 PM
  • You certainly have a string that is not in a correct format.

    You can also try int.TryParse instead of Convert.ToInt32.  It returns a bool, indicating whether or not parsing was successful.

    The exception comes from trying to convert text that is not a number.

    Some examples of strings that DO convert to Int32:

    • "    10    "
    • "\t10\n"
    • "-10"
    • "10\u0000"

    Some examples of strings that DO NOT convert to Int32:

    • "11,000"
    • "29.95"
    • "10.0"
    • "1 234 567"
    • "$10"
    • "10 cm"
    • "2A6B3F"
    • "10\u00000"


    Thursday, May 3, 2012 8:32 PM
  • You certainly have a string that is not in a correct format.

    You can also try int.TryParse instead of Convert.ToInt32.  It returns a bool, indicating whether or not parsing was successful.

    The exception comes from trying to convert text that is not a number.

    Some examples of strings that DO convert to Int32:

    • "    10    "
    • "\t10\n"
    • "-10"
    • "10\u0000"

    Some examples of strings that DO NOT convert to Int32:

    • "11,000"
    • "29.95"
    • "10.0"
    • "1 234 567"
    • "$10"
    • "10 cm"
    • "2A6B3F"
    • "10\u00000"


    Just to simplify the answer:

    The data type int can only hold integers, so no decimal points. Because of this the parser won't accept them. Also note that any none numeric charactors (numeric: 0,1,2,3,4,5,6,7,8,9 (not the ",")) will make that error too.


    MCP: ASP.NET
    Langauges: VB.net, C#, Java, C++ (non-CLI), ActionScript, JavaScript, (X)HTML, XAML, MXML

    Thursday, May 3, 2012 8:44 PM
  • plz will u give me an example how i use int.TryParse plz

    unseendreamzzz

    Thursday, May 3, 2012 8:52 PM
  • int number = 0; if (!int.TryParse(inputText.Text, out number))
    Console.WriteLine("invalid number);


    http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx

    MCP: ASP.NET
    Langauges: VB.net, C#, Java, C++ (non-CLI), ActionScript, JavaScript, (X)HTML, XAML, MXML

    • Proposed as answer by Papy Normand Thursday, May 3, 2012 10:22 PM
    • Marked as answer by unseendreamzzz Friday, May 4, 2012 1:31 PM
    Thursday, May 3, 2012 9:17 PM