0

I am creating a calculator (pic below) where there is a textbox that takes user inputs like 1+2 and then a button to calculate the sum. I created the Web Method to calculate the sum. It works fine when am taking values from two separate textboxes but now i am using a calculator interface with one textbox as shown in the pic below.

My problem is that when I am clicking the add button and invoking the function, 1+2, I am getting the error System.FormatException: 'Input string was not in a correct format.', see pic below. I tried to use 'TextBox.Text.Split' but i am not able to implement it.

WebMethod

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebServicesDemo
{
    /// <summary>
    /// Summary description for CalculatorWebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class CalculatorWebService : System.Web.Services.WebService
    {

        [WebMethod]
        public int Add(int firstNumer, int secondNumber)
        {
            return firstNumer + secondNumber;
        }
    }
}

Invoking the Web Service

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CalculatorWebApplication
{
    public partial class WebForm1 : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button5_Click(object sender, EventArgs e)

        {
            CalculatorService.CalculatorWebServiceSoapClient client = new CalculatorService.CalculatorWebServiceSoapClient();
            int result = client.Add(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox1.Text));
             txtAnswer.Text = result.ToString();


        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;

            TextBox1.Text = TextBox1.Text + button.Text;
        }
    }
}

Error message pic

Calculator interface

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Jorge00
  • 3
  • 1
  • What's the value of `TextBox1.Text` when the exception is thrown? "1+2"? – gunr2171 Jan 25 '22 at 18:30
  • yes the value is 1+2 – Jorge00 Jan 25 '22 at 18:43
  • Convert.xxx is for turning a string representation of a number into that number data type. "1+2" is a math equation, not a number. You can research ways of solving the math equation, or you can structure your app in a different way where you don't need to parse and understand the string contents. – gunr2171 Jan 25 '22 at 18:46
  • int.Parse will have exactly the same behavior as the Convert methods. It won't solve a math equation for you. You'll want to lookup a tutorial to follow, like [this one for WinForms](https://www.c-sharpcorner.com/article/create-basic-calculator-using-windows-forms-and-c-sharp/). The concepts of how to manipulate data for a calculator are language-agnostic and too broad for this Q&A format. – gunr2171 Jan 25 '22 at 18:52

0 Answers0