Answered by:
Input string was not in a correct format. How it can be possible.

Question
-
User1052662409 posted
Hi All,
I have a label in my form. It's has some value but still it causes error and shows message "Input string was not in a correct format. "
See the attachment below.
Thanks
Monday, January 18, 2016 11:45 AM
Answers
-
User-434868552 posted
the issue is NOT with the String you showed us:
String textValue = "3,750.00"; Double xFromText = 0; xFromText = Convert.ToDouble(textValue); Console.WriteLine(xFromText);
output:
3750
demoninside9, you need to change your strategy.
(a) use Double.TryParse* on every text field
(b) if ANY of the text fields fail conversion to Double, ask your end user to correct that data.
(c) only if ALL text fields have successfully converted to Double, then it will be safe to perform your comparison with the Double fields from step (a).
* https://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx "Double.TryParse Method (String, Double)"
BOTTOM LINE: at least one of your text fields contains bad data; it is the bad data that is crashing your code.
edit:
demoninside9
How it can be possible.https://msdn.microsoft.com/en-us/library/zh1hkw6k(v=vs.110).aspx "Convert.ToDouble Method (String)"
Convert.ToDouble can throw a FormatException: https://msdn.microsoft.com/en-us/library/system.formatexception(v=vs.110).aspx
"The exception that is thrown when the format of an argument is invalid, or when a composite format string is not well formed."
end edit.
edit #2:
another possibility is that your locale is mismatched.
references:
https://msdn.microsoft.com/en-us/library/h6270d0z(v=vs.110).aspx "Globalizing and Localizing .NET Framework Applications"
https://msdn.microsoft.com/en-us/library/syy068tk(v=vs.100).aspx "Formatting Numeric Data for a Specific Culture"
https://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numberdecimalseparator(v=vs.110).aspx
"NumberFormatInfo.NumberDecimalSeparator Property"end edit #2.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 18, 2016 7:26 PM -
User-821857111 posted
The parameter marked "out" should be the value that is parsed if the parsing is successful. In this case, a double. But the TryParse method returns a bool to indicate whether the parsing worked or not. So you should do this:
double advanceperdiem; if(Double.TryParse(txtAdvancePerdiem.Text, out advanceperdiem)) { //the operation was successful and advanceperdiem holds the parsed value } else { // the value being parsed is in an incorrect format }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 19, 2016 8:18 AM -
User753101303 posted
Just see the doc.
The first parameter is the input string, the second out parameter is the converted double value and it returns a boolean telling you if the conversion succeeded or not.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 19, 2016 8:38 AM -
User-434868552 posted
Please note: it is absolutely important to carefully study MSDN articles.
"Double.TryParse Method (String, Double)" https://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx
public static bool TryParse( // return a Boolean string s, // String containing a number to convert out double result // (a) ) // (a) result Type: System.Double // if the conversion succeeded: // contains the double-precision floating-point number // equivalent of the s parameter, // else zero
Carefully study the entire MSDN article!
demoninside9, since you have several fields, use a pattern like this with one Boolean per field:
Boolean advancePerdiemOkay = false; Boolean advanceHotelOkay = false; Boolean advanceInput3Okay = false;
Double advancePerdiem = 0;
Double advanceHotel = 0;
Double advanceInput3 = 0;
advancePerdiemOkay = Double.TryParse(txtAdvancePerdiem.Text, out advancePerdiem); advanceHotelOkay = Double.TryParse(txtAdvanceHotel.Text, out advanceHotel); advanceInput3Okay = Double.TryParse(txtAdvanceInput3.Text, out advanceInput3); if(advancePerdiemOkay && advanceHotelOkay && advanceInput3Okay) { if( (advancePerdiem > advanceInput3) || (advanceHotelOkay > advanceInput3)) { // actions for true } else { // actions for false } else { // actions for bad data // -- each Boolean value that is false should retrun // an appropriate ERROR MESSAGE to the end user }NOTE: you can NOT do this:
if(advancePerdiem || advanceHotel > advanceInput3)
You will get this compile time error: Operator '||' cannot be applied to operands of type 'double' and 'bool'
study this MSDN article: https://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx "7.2.1 Operator precedence and associativity"
> has higher precedence than || so advanceHotel > advanceInput3 is evaluated first, value is a Boolean
however advancePerdiem is a Double and Double || Boolean is illegal.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 19, 2016 9:20 AM
All replies
-
User1577371250 posted
Hi,
Check the values in all the labels or textboxes. Some value is wrong.
Monday, January 18, 2016 12:14 PM -
User753101303 posted
Hi,
String to non string conversion always depends on which country convention is currently in use. So the value you are showing could be wrong for the culture you are using even if it is would be right for some other culture.
So which culture do you use? Also are you sure this is this particuliar value that causes the issue or could it txtAdvanceHotel or any other value used on the same line?
If at this point in your code, you are not sure a user input value is correct you could use https://msdn.microsoft.com/en-us/library/system.double.tryparse(v=vs.110).aspx that returns a boolean so that you can check if the conversion failed or succeeded and show some message to the user to ask for a correct input.
Monday, January 18, 2016 12:31 PM -
User-2001765250 posted
Use Conversion methods to get and set value to that label. Like to get value use Convert.toString(label1.text) and to set the value to the label use label1.text = convert.toString(stringvalue);
Hope this helps
Monday, January 18, 2016 12:41 PM -
User-434868552 posted
the issue is NOT with the String you showed us:
String textValue = "3,750.00"; Double xFromText = 0; xFromText = Convert.ToDouble(textValue); Console.WriteLine(xFromText);
output:
3750
demoninside9, you need to change your strategy.
(a) use Double.TryParse* on every text field
(b) if ANY of the text fields fail conversion to Double, ask your end user to correct that data.
(c) only if ALL text fields have successfully converted to Double, then it will be safe to perform your comparison with the Double fields from step (a).
* https://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx "Double.TryParse Method (String, Double)"
BOTTOM LINE: at least one of your text fields contains bad data; it is the bad data that is crashing your code.
edit:
demoninside9
How it can be possible.https://msdn.microsoft.com/en-us/library/zh1hkw6k(v=vs.110).aspx "Convert.ToDouble Method (String)"
Convert.ToDouble can throw a FormatException: https://msdn.microsoft.com/en-us/library/system.formatexception(v=vs.110).aspx
"The exception that is thrown when the format of an argument is invalid, or when a composite format string is not well formed."
end edit.
edit #2:
another possibility is that your locale is mismatched.
references:
https://msdn.microsoft.com/en-us/library/h6270d0z(v=vs.110).aspx "Globalizing and Localizing .NET Framework Applications"
https://msdn.microsoft.com/en-us/library/syy068tk(v=vs.100).aspx "Formatting Numeric Data for a Specific Culture"
https://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numberdecimalseparator(v=vs.110).aspx
"NumberFormatInfo.NumberDecimalSeparator Property"end edit #2.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, January 18, 2016 7:26 PM -
User1052662409 posted
gerrylowry
demoninside9, you need to change your strategy.
(a) use Double.TryParse* on every text field
Sir, I tried as you suggested. But it shows it is a bool type, but I need those as double. I don't need any bool value.
double a, advanceperdiem; a = Double.TryParse(txtAdvancePerdiem.Text, out advanceperdiem);
Or may be I am confused and making some mistake.
Thank You
Tuesday, January 19, 2016 6:33 AM -
User-821857111 posted
The parameter marked "out" should be the value that is parsed if the parsing is successful. In this case, a double. But the TryParse method returns a bool to indicate whether the parsing worked or not. So you should do this:
double advanceperdiem; if(Double.TryParse(txtAdvancePerdiem.Text, out advanceperdiem)) { //the operation was successful and advanceperdiem holds the parsed value } else { // the value being parsed is in an incorrect format }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 19, 2016 8:18 AM -
User753101303 posted
Just see the doc.
The first parameter is the input string, the second out parameter is the converted double value and it returns a boolean telling you if the conversion succeeded or not.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 19, 2016 8:38 AM -
User-434868552 posted
Please note: it is absolutely important to carefully study MSDN articles.
"Double.TryParse Method (String, Double)" https://msdn.microsoft.com/en-us/library/994c0zb1(v=vs.110).aspx
public static bool TryParse( // return a Boolean string s, // String containing a number to convert out double result // (a) ) // (a) result Type: System.Double // if the conversion succeeded: // contains the double-precision floating-point number // equivalent of the s parameter, // else zero
Carefully study the entire MSDN article!
demoninside9, since you have several fields, use a pattern like this with one Boolean per field:
Boolean advancePerdiemOkay = false; Boolean advanceHotelOkay = false; Boolean advanceInput3Okay = false;
Double advancePerdiem = 0;
Double advanceHotel = 0;
Double advanceInput3 = 0;
advancePerdiemOkay = Double.TryParse(txtAdvancePerdiem.Text, out advancePerdiem); advanceHotelOkay = Double.TryParse(txtAdvanceHotel.Text, out advanceHotel); advanceInput3Okay = Double.TryParse(txtAdvanceInput3.Text, out advanceInput3); if(advancePerdiemOkay && advanceHotelOkay && advanceInput3Okay) { if( (advancePerdiem > advanceInput3) || (advanceHotelOkay > advanceInput3)) { // actions for true } else { // actions for false } else { // actions for bad data // -- each Boolean value that is false should retrun // an appropriate ERROR MESSAGE to the end user }NOTE: you can NOT do this:
if(advancePerdiem || advanceHotel > advanceInput3)
You will get this compile time error: Operator '||' cannot be applied to operands of type 'double' and 'bool'
study this MSDN article: https://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx "7.2.1 Operator precedence and associativity"
> has higher precedence than || so advanceHotel > advanceInput3 is evaluated first, value is a Boolean
however advancePerdiem is a Double and Double || Boolean is illegal.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 19, 2016 9:20 AM -
User1052662409 posted
Thank you all : gerrylowry, Mikesdotnetting and PatriceSc.
Tuesday, January 19, 2016 9:57 AM