Simple Calculator using Asp.net Web service

Geetha.Katneni
Posted by Geetha.Katneni under ASP.NET category on | Points: 40 | Views : 13306
Creating Simple Calculator using Web services[ /B]

1)Go To Visual Studio 2010 -> New Project -> Select Asp.NET Web Service Application -> OK
2) In Service.asmx.cs -put the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace Session_WebService1
{
/// <summary>
/// Summary description for Service1
/// </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 Service1 : System.Web.Services.WebService
{

// Simple Calculator //

[WebMethod]
public double Add(double x, double y)
{
return x + y;
}
[WebMethod]
public double Sub(double x, double y)
{
return x - y;
}
[WebMethod]
public double Mult(double x, double y)
{
return x * y;
}
[WebMethod]
public double Div(double x, double y)
{
return x / y;
}
[WebMethod]
public double Pow(double x, double y)
{
double PowVal = x;
for (int i = 0; i < y - 1; i++)
{
PowVal *= x;
}
return owVal;
}
}
}

3) a) Add NewCalci.aspx page

Design the form with- 3 TextBoxes with Names TxtValue1,TxtValue2,TxtResult to enter value1,value2 and result.
5 Buttons with Names BtnAdd , BtnSub , BtnMul ,BtnDiv , BtnPow , BtnClear.



4)Write the Following code in NewCalci.aspx.cs file

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

namespace Session_WebService1
{
public partial class NewCalci : System.Web.UI.Page
{
// instantiate the web service proxy
Service1 CalciService = new Service1();



protected void BtnAdd_Click(object sender, EventArgs e)
{

double var1 = Convert.ToDouble(TxtValue1.Text);
double var2 = Convert.ToDouble(TxtValue2.Text);

// call the add method

TxtResult.Text = Convert.ToString(CalciService.Add(var1, var2));

}

protected void BtnSub_Click(object sender, EventArgs e)
{
double var1 = Convert.ToDouble(TxtValue1.Text);
double var2 = Convert.ToDouble(TxtValue2.Text);

// call the Sub method

TxtResult.Text = Convert.ToString(CalciService.Sub(var1, var2));
}

protected void BtnMul_Click(object sender, EventArgs e)
{
double var1 = Convert.ToDouble(TxtValue1.Text);
double var2 = Convert.ToDouble(TxtValue2.Text);

// call the Mul method

TxtResult.Text = Convert.ToString(CalciService.Mult(var1, var2));
}

protected void BtnDiv_Click(object sender, EventArgs e)
{
double var1 = Convert.ToDouble(TxtValue1.Text);
double var2 = Convert.ToDouble(TxtValue2.Text);

// call the Div method

TxtResult.Text = Convert.ToString(CalciService.Div(var1, var2));
}


protected void BtnPow_Click(object sender, EventArgs e)
{
double var1 = Convert.ToDouble(TxtValue1.Text);
double var2 = Convert.ToDouble(TxtValue2.Text);

// call the Power method

TxtResult.Text = Convert.ToString(CalciService.Pow(var1, var2));
}

protected void BtnClear_Click(object sender, EventArgs e)
{
TxtValue1.Text = "";
TxtValue2.Text = "";
TxtResult.Text = "";

}



}
}

5) Run the Web service by pressing F5,Check the reult.

Comments or Responses

Login to post response