Input string was not in a correct format. on string "0"

C

chongo

Hi,

i'm reading in a bigint value from SQL Server.

string s = myReader["BackColour"].ToString());

int intBackColour = System.Convert.ToInt32(s);

I'm also reading in other values the same way and it works great
except this one value, i'm getting
System.FormatException: Input string was not in a correct format.

I don't understand because when I print out the string s to the screen
it shows 0.

Does anyone have a idea why its doing this?

Any help would be greatly appreciated.
-dp
 
A

ashelley

Hi,

i'm reading in a bigint value from SQL Server.

string s = myReader["BackColour"].ToString());

int intBackColour = System.Convert.ToInt32(s);

I'm also reading in other values the same way and it works great
except this one value, i'm getting
System.FormatException: Input string was not in a correct format.

I don't understand because when I print out the string s to the screen
it shows 0.

Does anyone have a idea why its doing this?

Any help would be greatly appreciated.
-dp

Very strange.

Try

int intBackColour = int.Parse(s);

instead of

int intBackColour = System.Convert.ToInt32(s);

-Adam
 
E

Edward

Yes, remember to use Convert.ToInt32 carefully, sometimes it doesn't work to
String convert , while Int32.Parse is ok.

I guess, if two types are convertable, such as Int to BigInt, or Object
to Int, System.Convert.Toxxx can work. String to Int is not
convertable, so Convert.Toxxx may be wrong sometime.

you can check MSDN for the types relation. That will give you details.
Hi,

i'm reading in a bigint value from SQL Server.

string s = myReader["BackColour"].ToString());

int intBackColour = System.Convert.ToInt32(s);

I'm also reading in other values the same way and it works great
except this one value, i'm getting
System.FormatException: Input string was not in a correct format.

I don't understand because when I print out the string s to the screen
it shows 0.

Does anyone have a idea why its doing this?

Any help would be greatly appreciated.
-dp

Very strange.

Try

int intBackColour = int.Parse(s);

instead of

int intBackColour = System.Convert.ToInt32(s);

-Adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top