Question

I really don't have the idea on why I am getting 0 value on this:

enter image description here

But this code works well:

int val = Convert.ToInt32("1546");    

Here is the appsetting:

<add key="PesoPayMerchantId" value="​1546"/>

Any idea?

Edit1

I want to get the integer value of "1546", but it fails to work.
Here is the code for getting appsetting:

    public static string GetConfigurationString(string appSettingValue)
    {
        return ConfigurationManager.AppSettings[appSettingValue];
    }  

I have tried your suggestions, and this is the result:

enter image description here
enter image description here
enter image description here

The string value is correct ("1546"), but it can't be parse to integer. What is happening here?

Edit 2

I am very sure that the value of:

<add key="PesoPayMerchantId" value="​1546"/>  

is really a combination of numbers "1546"
But when I try to re-write the string value using Immediate Window it can now be parsed. But still I can't figure out the very reason of this Bug?

enter image description here

Edit 3

Finally, it works now, thanks to Johnny

What I did is, I re-write the whole, <add key="PesoPayMerchantId" value="1546"/> and it can now be parsed. Thanks for all your help. :D

Was it helpful?

Solution

The answer would be, re-write the config.
As I remember, I just copied and paste "1546" from a pdf file.
So lesson learned, don't be too lazy on typing values.

Additional Information:
I also remember that, I did copy and paste on gmail (google Chrome) and I found out that the text I copied contains hidden characters at the beginning.

OTHER TIPS

I can only think that you are experiencing some kind of weird globalization / Culture specific problem.

Given that you know the exact format of the number, you might try the Int32.TryParse Method (String, NumberStyles, IFormatProvider, Int32) overload, e.g.:

int.TryParse(val, NumberStyles.Any, CultureInfo.InvariantCulture, out id);

I would inspect the return value of Try.Parse.

From documentation: http://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx

      int number;
      bool result = Int32.TryParse(value, out number);
      if (result)
      {
         Console.WriteLine("Converted '{0}' to {1}.", value, number);         
      }
      else
      {
         if (value == null) value = ""; 
         Console.WriteLine("Attempted conversion of '{0}' failed.", value);
      }

This test Assert's always correctly:

namespace SOWTests
{
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class PTests
    {
        [TestMethod]
        public void PTest()
        {
            string val = "1546";

            int id;
            int.TryParse(val, out id);

            Assert.AreEqual(1546, id);
        }
    }
}

So problem not in this part of code. It's maybe altered by some debugging/profiling part of your code. Or maybe there some stack corruption from unmanaged call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top