Home > Net >  Weird FormatException with Convert.ToInt32
Weird FormatException with Convert.ToInt32

Time:10-04

I am getting a frustratingly weird Form

enter image description here

I've also tried changing it to job.Tags |= Convert.ToInt32(item, CultureInfo.InvariantCulture) but even that gives the same exception.

I have absolutely no idea why.

Here is a character representation of item
enter image description here

And here is my code

if (model.TagSelection != null)
{
    foreach (var item in model.TagSelection)
    {
        job.Tags |= Convert.ToInt32(item, CultureInfo.InvariantCulture);
    }
}

CodePudding user response:

Item Length is 5 - the numeric characters are delimited by double quotes (as you'd code but not expect to see in captured data). Since you are getting the string "256", you'll have to Replace() them with string.Empty to get a numerics-only string length 3, which should then convert.

  var s = "\"256\"";
  var s2 = s.Replace("\"", string.Empty);

Edit: I just realized it's worse than it appears. I think those are left- and right-slanted quotes (note the character codes 823x). You may need something more like this...

var s3 = new string(s.Where(char.IsDigit).ToArray());

CodePudding user response:

The issue has come from copying the values from Excel, not sure why it is doing it but after deleting it and retyping it the error has gone away.

  • Related