Last post Nov 01, 2010 03:08 PM by SGWellens
Nov 01, 2010 11:38 AM|paminchever|LINK
anyone can help me regarding this error:
Exception Details: System.FormatException: Input string was not in a correct format.
here's my code behind:
referenceno = Convert.ToInt32(drReaderpay["STAFF_ID"]);
note: staff_id = 123456-8
Nov 01, 2010 11:40 AM|amolbehere|LINK
Use below code
referenceno = Convert.ToInt32(drReaderpay["STAFF_ID"].ToString());
Nov 01, 2010 11:41 AM|XIII|LINK
first replace the - with an .
string temp = drReaderpay["STAFF_ID"].ToString().Replace('-','.');
referenceno = Convert.ToInt32(temp);
Nov 01, 2010 11:45 AM|paminchever|LINK
what if the staff id doesn't have the - sign...
for ex. staff id = 123456-7 or 1234567
Nov 01, 2010 11:47 AM|iGulfam|LINK
Use following statement for conversion
referenceno = Convert.ToInt32(drReaderpay["STAFF_ID"].ToString().Replace("-",""));
Nov 01, 2010 11:55 AM|paminchever|LINK
thanx for the help but its still NOT working got no error but the problem is staff id is not correct...
for ex. staff id = 911566-2 or staff id = 9115662
Nov 01, 2010 11:59 AM|Paul Linton|LINK
123456-8 is not an integer, it is not even a number.
What do you want the answer to be? I can think of a few possible answers
123456 (discard everything after the first non-digit)
8 (discard everything before the last non-digit)
1234568 (discard all non-digits)
123456.8 (treat a non-digit as a decimal place)
123457 (treat a non-digit as a decimal place and round to an integer)
123448 (treat the string as an expression and actually perform the subtraction)
42798 (treat the piece after a non-digit as the base for the number before the non-digit and convert to decimal!) - took a little effort to dream up that one.
123456, 123457, 123458 (maybe it specifies a range of numbers)
??? (somthing else that I can't think of)
We have to know what you are trying to achieve before we can tell you how to write the code to do it!
Nov 01, 2010 12:00 PM|XIII|LINK
911566-2 or staff id = 9115662
What does it need to be?
I suggest you simply call different Replace statements like str.Replace('-','') for example until the number as a string is in a correct format. We can't see all different types of possible staff id's in your database so you have to be a bit creative on
Nov 01, 2010 12:04 PM|paminchever|LINK
i use this code:
referenceno = Convert.ToInt32(drReaderpay["STAFF_ID"].ToString().Replace("-", ""));
some of the staff id doesn't have the (-) sign and its working fine.. but if the staff id has the (-) got this error...
Nov 01, 2010 12:08 PM|Paul Linton|LINK
So what other non-digits are present in the string? You can only convert something to an int if it is made up entirely of digits. If you want to ignore the non-digits then Replace them.
Nov 01, 2010 12:12 PM|amolbehere|LINK
I guess in this you should stick with data type of column 'STAFF_ID' in database which must be varchar.
Is your code making manipulations based on 'STAFF_ID' as integer?
Nov 01, 2010 12:16 PM|paminchever|LINK
my STAFF_ID in my database is varchar...
and dnt have any code making manipulations on staff id as integer...
sorry im new in .net anyone can help me!
Nov 01, 2010 12:21 PM|dk_4_asp|LINK
referenceno = Convert.ToInt32(drReaderpay["STAFF_ID"].ToString().Replace("-", " ").Trim());
Nov 01, 2010 12:23 PM|amolbehere|LINK
Then solves the issue itself.
Generally we do keep columns and variable datatypes in sync. So I think you can remove Convert.ToInt32 and instead assign the value to a string variable.
Nov 01, 2010 12:37 PM|praveshsms|LINK
and also replace the values other then integer in Staff_id with the same Replace method
Nov 01, 2010 03:08 PM|SGWellens|LINK
If STAFF_ID is indeed an integer, then you have dirty, corrupt data.
To change the field type to int you: