.NET Framework Bookmark and Share   
 index > .NET Base Class Library > input string was not in a correct format
 

input string was not in a correct format

Ehy guys i have these instructions :
int richiesta = Convert.ToInt32(parametri[0]);
string unitaorg = Convert.ToString(parametri[1]);
int anno = Convert.ToInt32(parametri[2]);
I have the input error on the first one...Why?

Help me :)
input string was not in a correct format
What's the value of "parametri[0]"? Does it look like an integer value?
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
David M Morton
No, it's a string. :)
input string was not in a correct format
Sure, I understand that.... but what's the content of the string? What's the string's value not it's type.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
David M Morton
the string's value is 'XXNNXX'
input string was not in a correct format
Let me ask you a question then...

If I asked you this question:

What number is best represented by thecharacters"XXNNXX"?

What would you say?

You'd probably say I was crazy. "XXNNXX" isn't anumber at all... it's a bunch of letters.

That's what you're asking the computer to do, however, by asking it to convert to an integer value.

To convert to Int32 from string, your string has to be a series of numbers.... like "123" or "456" or "1", etc.

Does that make sense?
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
David M Morton
Let me ask you a question then...

If I asked you this question:

What number is best represented by thecharacters"XXNNXX"?

What would you say?

You'd probably say I was crazy. "XXNNXX" isn't anumber at all... it's a bunch of letters.

That's what you're asking the computer to do, however, by asking it to convert to an integer value.

To convert to Int32 from string, your string has to be a series of numbers.... like "123" or "456" or "1", etc.

Does that make sense?
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
David M Morton
sorry...i'm crazy..not you:)..i have controlled and finally i'll says you that parametri[0] is a number '1' '2' and so on ...NOT 'XXNNXX'..do you understand ?

Sorry :)
input string was not in a correct format
So is it fixed? If so, mark one of the helpful posts as answer.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
David M Morton
..however i don't understand why dont reach the conversion with this instruction...help meeee :)
input string was not in a correct format
Could you be more specific?

If the error you're receiving is "input string was not in a correct format", there's something wrong with the string, in other words, it's not a number represented in the string at all.

Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki �LinkedIn �ForumsBrowser
David M Morton

Hi,
Are you still troubled by the exception?
If I encounter such error when convert a string to an int value, I will set a breakpoint at that line, run the program, when the breakpoint is hit, check the value of parametri[0] to make sure it is a sequence of digits.
By the way, Int32.TryParse method provides a friendly way to do the conversion, a "false" instead of an exception will be returned if the given string is not a valid int value.

Thanks,
Eric


Please remember to mark helpful replies as answers and unmark them if they provide no help.
eryang

You can use google to search for other answers

Custom Search

More Threads

• Impersonation
• Getting lenth of an array
• How can i create a G-L-O-B-A-L hook in c#?
• Serialization with custom binder tries to load type from wrong assembly
• Convert string to SystemColor?
• Strange Issue with "Split"
• Sockets - How to work with async data?
• office.interop.excel - Office11 and Office12 on Development PC
• What does the grave signify?
• How to check if the logged on user is an administrator?