-1

I am working on a little project, that at a certain point need to convert a string to int.

I tried the following things:

//first try
value1 = int.TryParse(value[0].tostring(), out i)

//second
value1 = Convert.ToInt32(value[0].tostring())

//third
value1 = int.Parse(value[0].tostring())

I even wrote my own conversion method because I was at a loss.

The values I am trying to convert are queried from a MySQL database.

Thank for your help

EDIT:

I know tryparse should have 2 params.

And the error iam getting is a formatexception Input string was not in a correct format.

I got that on all the tries.

the value in my test case is 2500 Keep in mind that that number is received from a db I tried the above snippets while using a hard coded value. And that works fine.

EDIT 2: the values //http://imgur.com/NSyg2rJ

WillemT
  • 88
  • 10

2 Answers2

0

Try this -

int value1;
if (Int.TryParse(value[0].ToString(), out value1))
{
    //conversion successful
}
dynamicuser
  • 1,522
  • 3
  • 24
  • 52
0

One: The signature of int.TryParse() is incorrect:

int result = 0;
bool parsedSuccessfully = Int32.TryParse(value[0].ToString(), out result);
//If successful, result will hold the value.

Two: The signature of Convert.ToInt32() is correct, minus the case of the .ToString() method. This is failing because value or value[0] is null, or it cannot convert something like the string "NotAnInt" to an int.

Three: The signature of int.Parse() is correct, but is failing for the same reasons as two.

DJH
  • 2,191
  • 4
  • 28
  • 45
  • Thanks for your reaction. i typoed the tryparse method. of cousre there should be 2 params. As for the value in values[0] that is 2500, so that shouldn't be a problem. Right? – WillemT Dec 13 '13 at 15:05
  • If values[0] is 2500 it should work. Does that not work for you? – DJH Dec 13 '13 at 15:12
  • Nope it doesnt... I know it weird... – WillemT Dec 13 '13 at 15:25
  • Iam lossing my mind over this – WillemT Dec 13 '13 at 15:25
  • What is the exact type and value of values[0]? I assume 2500 is the value. Are you sure there are no illegal characters in the input string that could make it fail? Try [specifying the format provider](http://stackoverflow.com/questions/7532801/system-formatexception-input-string-was-not-in-a-correct-format) and see if that still fails. – DJH Dec 13 '13 at 15:33
  • oh wauw, i just printed the string length... 2500 should have a string length of 4... The string was length 5 – WillemT Dec 13 '13 at 15:51
  • so how can i see the "missing" char – WillemT Dec 13 '13 at 15:52
  • Try using something like [this](http://stackoverflow.com/a/11138139/1229237) on your input string. I'm a little out of my depth now, but I believe you can have non-printable characters in your string. This could be preventing the conversion. But it's only a guess. – DJH Dec 13 '13 at 16:05
  • Stupid me there is an euro sign in the string. it is received from db. which is a latin db so maybe that changes things – WillemT Dec 13 '13 at 16:36
  • Well there's your problem. However, [you can convert with the currency symbol in the string](http://stackoverflow.com/questions/4094334/format-currency-string-to-integer). – DJH Dec 13 '13 at 16:46
  • the problem is of course that the symbol is not visible when debugging and i tried the method you gave me. but it still doesnt work – WillemT Dec 13 '13 at 17:27
  • please check the images included on my post that is what it looks like – WillemT Dec 13 '13 at 17:57
  • Did you remove the symbol from the string? If you did then I am out of ideas... – DJH Dec 13 '13 at 18:05
  • Thanks for your help. I fixed it by removing the euro sign in the database – WillemT Dec 19 '13 at 07:30