-1

I'm trying to convert text from a textbox to an int, which is then used as a parameter when calling a method from a webservice. I'm getting this error:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

Any help?

Heres my code

            int amount= int.Parse(txtAmount.Text);

            Service1Client connectionToService = new Service1Client();

            lblResult.Text = connectionToService.getCurrencyName(amount, "Rupee", "Euro");
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Ger Mc
  • 630
  • 3
  • 11
  • 22
  • 2
    Where is the *text* that caused the exception? If you try to parse `abcd` you'll get exactly this exception. If you used logging, you'd see that the actual error message is `FormatException: Input string was not in a correct format.` – Panagiotis Kanavos Apr 28 '17 at 13:20
  • The text I'm entering is actually numbers so I'm guessing thats causing the conflict? – Ger Mc Apr 28 '17 at 13:21
  • Post the string. If you get this error, you aren't using the correct number format *for the current locale*. If you want to parse text using a specific locale, specify this by passing a `CultureInfo` object – Panagiotis Kanavos Apr 28 '17 at 13:22
  • Possible duplicate of [String to int formatexception](http://stackoverflow.com/questions/19550940/string-to-int-formatexception) – Panagiotis Kanavos Apr 28 '17 at 13:26
  • Post the string, or this question will have to be closed. It's impossible to guess what's wrong - unless you are typing a decimal instead of an actual integer. Don't force people to guess what you tried. – Panagiotis Kanavos Apr 28 '17 at 13:29
  • `123456` is an integer. `123.456` is a decimal and can't be parsed with `int.Parse`. So is `123,456`. If you really entered an integer, you wouldn't get an error. Post the input string – Panagiotis Kanavos Apr 28 '17 at 13:31
  • its been answered thanks. – Ger Mc Apr 28 '17 at 13:32
  • No it hasn't. That wasn't an answer and will probably get deleted very soon. That would raise the exact same error. Post the input. You *can't* parse letters. The only way this works is if you *changed* the input to something that can also be parsed with int.Parse – Panagiotis Kanavos Apr 28 '17 at 13:40
  • In fact, [Convert.ToInt32](https://github.com/Microsoft/referencesource/blob/master/mscorlib/system/convert.cs#L1112) calls `int.Parse`. If your input works with `Convert.ToInt32` it means that it works with `int.Parse` – Panagiotis Kanavos Apr 28 '17 at 13:46
  • @PanagiotisKanavos yes you are right. – user7415073 Apr 28 '17 at 14:03
  • @PanagiotisKanavos and i tried to delete the post but i can't because it was accepted. so it will automatically deleted if any two downs it. so please cool. and try to find the answer. – user7415073 Apr 28 '17 at 14:05

3 Answers3

3

Various things to do here:

Either make sure your textbox only accepts numbers (for example start with 0 and look for any inputs other than 0-9).

If you are parsing other values than the above always use the Double parsing since single characters like 1⁄2, 3⁄4 etc can indeed be parsed but only as Double values.

Use TryParse instead of Parse.

double parsedValue;
bool wasParsedCorrectly = Double.TryParse(txtTest.Text, out parsedValue);

If this returns true your input is ok, if not check the input. The Double value will then be in the parsedValue variable. This is better than having to deal with the actual exception.

If you expect wrong inputs you can use try / catch with .Parse but with TryParse this will not be needed and is the more readable solution

Marc Wittmann
  • 2,286
  • 2
  • 28
  • 41
1

it's better to put this in a try/catch

try() 
{
    int amount= int.Parse(txtAmount.Text);
    Service1Client connectionToService = new Service1Client();
    lblResult.Text = connectionToService.getCurrencyName(amount,   
       "Rupee","Euro");
} catch(FormatException e) 
{
    //Treatment
}
-6

can you please try

Convert.ToInt32("textbox.text")
user7415073
  • 290
  • 4
  • 22
  • The string `"textbox.text"` will throw. The statement `Convert.ToInt32(textbox.text)` will throw the same exception, because it *also* tries to parse the text – Panagiotis Kanavos Apr 28 '17 at 13:25
  • 2
    No way this works. The string `"textbox.text"` isn't a number. *What is the input string* that causes the exception ? – Panagiotis Kanavos Apr 28 '17 at 13:26
  • This isn't an answer even if the OP fixed the typpo. Using `Convert.Int32` actually [calls `int.Parse`](https://github.com/Microsoft/referencesource/blob/master/mscorlib/system/convert.cs#L1112). The only way this would work is if the OP changed the input – Panagiotis Kanavos Apr 28 '17 at 13:47
  • @GerMc This cannot possibly be the right answer. There is no way this worked for you... – Siyual Apr 28 '17 at 14:26