none
Input string was not in a correct format. RRS feed

  • General discussion

  • I have an app with over 100 users and 2 are getting errors. I have created a simple test app for them to run that identifies the problem.  And the 2 user that can not use my app get the following error with the test app:

    Input string was not in a correct format.

    The code:

      static void Main(string[] args) {  
       string a = null;  
       try { a = ConfigurationManager.AppSettings["TestString"]; }  
       catch (Exception ex) { Console.WriteLine(ex.ToString()); }  
       try {  
        Console.WriteLine("Running --> int b = int.Parse(a);");  
        int b = int.Parse(a); // This line throws the error.  
       }  
       catch (Exception ex) { Console.WriteLine(ex.ToString()); }  
       Console.WriteLine("Done");  
       Console.WriteLine("Press any key to exit");  
       Console.Read();  
      } 


    The app.config:

    <?xml version="1.0" encoding="utf-8" ?> 
    <configuration> 
     <appSettings> 
      <add key="TestString" value="0"/>  
      </appSettings> 
    </configuration> 


    This a .NET 2.0 app compiled useing Visual Studio 2005.  The main app is a clickonce deployment app that installs fine but throws the same error as this sample app

    Any Ideas on what may be wrong?

    Thanks,
    Erik
    • Changed type Zhi-Xin Ye Thursday, November 13, 2008 10:27 AM change to comment since no response for more than 3 days
    Friday, November 7, 2008 12:20 AM

All replies

  • Perhaps two of the users have really unusual internationalization/regional settings in their Control Panel (you can do Start, Run, "control international" to quickly bring up this Control Panel).

    That would be the case if the following code works:

    int b = int.Parse(a, System.Globalization.CultureInfo.InvariantCulture); 

    By the way, when reading configuration, it would be a good idea to always specify InvariantCulture so that you are immune to such differences in system configurations.
    Friday, November 7, 2008 12:30 AM
  • We have already checked this, just incase we missed something I modified the line of code as you suggested.  I will have the users test again.  The real app is using Spring .Net I am not reading the config file directly.

    I will update with another post if this change actually fixes the problem.

    Thanks for the quick reply.  Any other ideas?

    Erik
    Friday, November 7, 2008 12:54 AM
  • If you haven't done so already, you will probably also want to do a debugging print of the string "a" prior to calling Parse on it.  This will give more insight into what is going on.
    Friday, November 7, 2008 1:09 AM
  • I already did the following in the original app:

    In the Spring .Net code I added something like:

    Debug.WriteLine(string.Format("value=[{0}]",value)); 

    To verify that it was infact just the charcter 0 that was being passed to int.parse
    Friday, November 7, 2008 1:26 AM
  • And you are sure that is a zero in the file, not the letter O... right?
    Friday, November 7, 2008 1:57 AM
  • That aclually really looks like a locale issue.

    I know it may sound silly but... How does the test app reacts if you change the testvalue from 0 to 1 ?

    Friday, November 7, 2008 2:37 PM
  • It looks like:
    int b = int.Parse(a, System.Globalization.CultureInfo.InvariantCulture);   
     was the answer.

    We must have missed something when checking the users machines. I guess we need to check them again.

    Thanks for the help.  I will post solution once I find it.
    Friday, November 7, 2008 3:57 PM
  • I think some of us would be curious as to what exactly was in the region settings.

    It would seem quite odd that there would be some configuration where the single character 0 does not parse to zero.

    Let us know what you find out!

    Friday, November 7, 2008 11:49 PM
  • We were able to solve the issue by resetting the regional settings (changing to another language in the regional setting and languages option in the control panel, and then changing back to english us). We followed the following post:

    http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/4f52ff21-2afe-41c8-aa4e-61acd32dc4a2/

    Thursday, November 13, 2008 5:29 PM