Input string was not in a correct format

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
Gayathri GrandheAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
I would guess that
txtFirstNumber.Text or txtSecondNumber.Text
is failing.
0
Fernando SotoRetiredCommented:
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.
0
Chris StanyonWebDevCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Gayathri GrandheAuthor Commented:
thanks for providing comments. i used tryparse.
0
Fernando SotoRetiredCommented:
So did you find the issue?
0
Gayathri GrandheAuthor Commented:
yes, i found out that the issue is causing in int.parse and i rechanged to int.tryparse.
0
Fernando SotoRetiredCommented:
That is great. Please close this question by selecting those solutions that helped.

Thanks
Fernando
0
AndyAinscowFreelance programmer / ConsultantCommented:
I'd suggest you split over the first three comments.
0
Gayathri GrandheAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.