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;
}
}
}