Input string was not in a correct format

ghj Grandhe
ghj Grandhe used Ask the Experts™
on
Getting Input String Was not in a correct Format
.Actually I had written a method in webservice as method name as caculator
 [WebMethod]
   public int caculator(int firstNumber,int secondNumber,string operation)
    {
        int result = 0;
        switch(operation)
        {
            case "+": result = firstNumber + secondNumber;
                break;
            case "-": result = firstNumber - secondNumber;
                break;
            case "*": result = firstNumber * secondNumber;
                break;
            case "/":result = firstNumber / secondNumber;
                break;
        }
        return result;
    }
and Iam consuming this in webapplication where I have written as
private void BindData()
        {
            string operation = txtOperation.Text;
            WebService1 objWebService = new WebService1();
            int result=objWebService.caculator(int.Parse(txtFirstNumber.Text), int.Parse(txtSecondNumber.Text), operation);
            if (result != 0)
                Response.Write(result);
        }
 in this iam getting error at this line
int result=objWebService.caculator(int.Parse(txtFirstNumber.Text), int.Parse(txtSecondNumber.Text), operation);

i doesn't understand why iam getting error here. Even though i converted it into string
could you please suggest as soon as possible
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant
Commented:
I would guess that
txtFirstNumber.Text or txtSecondNumber.Text
is failing.
Fernando SotoRetired
Distinguished Expert 2017
Commented:
To Add to @AndyAinscow answer if txtFirstNumber.Text or txtSecondNumber.Text had a value like 1.0 then you would get that error.  Also having alpha with digits will cause the issue.
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
int.Parse() will throw the "Input string was not in a correct format." if it can't convert the value to an integer. It will also throw Exceptions if the value is null, or there's an overflow (too big a number). You have a couple of options on how to deal with it, depending on your logic.

You can wrap your code in a try catch and handle the exception:

try {
    ...
    int result=objWebService.caculator(int.Parse(txtFirstNumber.Text), int.Parse(txtSecondNumber.Text), operation);
    ...
} catch (Exception e) {
    // log e
    Response.Write("Could not convert the values");
}

Open in new window

or you can use the TryParse method instead. The TryParse doesn't throw an Exception. You pass in an out parameter and if it fails to convert, it will simply set the out parameter to 0 and return false:

var first, second;
int.TryParse(txtFirstNumber.Text, out first);
int.TryParse(txtSecondNumber.Text, out second);
int result=objWebService.caculator(first, second, operation);

Open in new window


Now if the string can't be converted, the value will just be set to 0 and your calculator method would be called. This may make sense or you can check the result of TryParse:

var first, second;
if ( int.TryParse(txtFirstNumber.Text, out first) && int.TryParse(txtSecondNumber.Text, out second) ) {
    ...
    int result=objWebService.caculator(first, second, operation);
    ...
} else {
    Response.Write("Could not convert the values");
}

Open in new window

Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Author

Commented:
thanks for providing comments. i used tryparse.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
So did you find the issue?

Author

Commented:
yes, i found out that the issue is causing in int.parse and i rechanged to int.tryparse.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
That is great. Please close this question by selecting those solutions that helped.

Thanks
Fernando
AndyAinscowFreelance programmer / Consultant

Commented:
I'd suggest you split over the first three comments.

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial