Вы находитесь на странице: 1из 7

CREATE A COMPONENT THAT RECEIVES TWO NUMBERS FROM THE USER THROUGH

A WEB FORM, AND BASED ON THE USERS SELECTION ADD OR SUBTRACT THE TWO
NUMBERS AND RETURNS THE RESULT TO THE WEB FORM. THE RESULT SHOULD BE
DISPLAYED IN THEWEB FORM USING ASP.NET
Step -1 First open your visual studio --> File -->New -->Select ASP.NET Empty Website and
select Visual C# from left window -->OK-->Now Open Solution Explorer-->Now Right click on
website --> Add New Items --> Select web service & Visual C# --> Write your web service's
name (calculationservices.asmx) --> click Add button as shown below:-

Step -2 Now Write the C# logic codes for calculation under [WebMethod] as given below:using
using
using
using
using

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

/// <summary>
/// Summary description for calculationservices
/// </summary>

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 calculationservices : System.Web.Services.WebService {
public calculationservices () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public int add(int i, int j)
{
return i + j;
}
[WebMethod]
public int substract(int i, int j)
{
return i - j;
}
[WebMethod]
public int multiply(int i, int j)
{
return i * j;
}
[WebMethod]
public int Division(int i, int j)
{
return i / j;
}
[WebMethod]
public int modulos(int i, int j)
{
return i % j;
}
}

Step -3 Now Run this web services (calculationservices.asmx) -->Now Right Click Website in
Solution Explorer --> Select Build Website
Next Right Click Website in Solution Explorer --> Select View in Browser
Now Select the file calculationservices.asmx

Step -4 Now you can calculate your services(Addition,Subtraction,Multiplication,Division and


modulus ) -->click Division link or others as show below:-

Step -5 Now click Invoke Button -->you will see ,output will show in XML Document format
using SOAP Protocol.

Step -6 Now open your visual studio again--> File -->New -->Select ASP.NET Empty Website
and select Visual C# from left window -->OK-->Now Open Solution Explorer-->Now Right
click on website --> Add New Web Form (webservice.aspx) --> Drag and drop Label ,Text Box
and Button control on the Form as shown below:-

Note :

web service extension --> .asmx

Web Form Extension -->.aspx

Disco File Extension -->.disco

Step -7 Now open your solution Explorer -->Right Click on your application (website) -->Add
Web Reference -->Paste web service URL here (Which i have already copied in step 3) -->Click
Next Arrow as show below:-

Note :- You can copy the URL by Run the web service again.
Step -8 Now Write the web service'Name (myservice) -->Click Add Reference Button.
as shown below :-

Step -9 You will see ,there are some files are added in your application(when we complete the
web service configuration) as shown below:-

Note:- See above image carefully:

Include a Name space in webservice.aspx.cs file as Using myservice;

Create object of calculationservices in webservice.aspx.cs file as calculationservices obj = new


calculationservices();.

Step -10 Now write the c# codes on button click in webservice.aspx.cs file by double clicking the
button as given below:using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using myservice;
public partial class webservice : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
calculationservices obj = new calculationservices();
//you can use below without include Namespace(using myservice;)
//myservices.calculationservices obj = new myservices.calculationservices();
int sum = obj.add(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
Label1.Text = "Your addition Result =" + sum.ToString();
//you can use below script instead of Label control
//Response.Write("<script> alert(" + sum + ")</script>");
}

protected void Button2_Click(object sender, EventArgs e)

calculationservices obj = new calculationservices();


int sub = obj.substract(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
Label1.Text = "Your Substraction Result =" + sub.ToString();
//you can use below script instead of Label1 control
//Response.Write("<script> alert(" + sub + ")</script>");

}
protected void Button3_Click(object sender, EventArgs e)
{
calculationservices obj = new calculationservices();
int mult = obj.multiply(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
//Response.Write("<script> alert(" + mult + ")</script>");
Label1.Text = "Your Multiplication Result =" + mult.ToString();
}
protected void Button4_Click(object sender, EventArgs e)
{
calculationservices obj = new calculationservices();
int div = obj.Division(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
Label1.Text = "Your Divison Result =" + div.ToString();
//you can use below script instead of Label1 control
//Response.Write("<script> alert(" + div + ")</script>");
}
protected void Button5_Click(object sender, EventArgs e)
{
calculationservices obj = new calculationservices();
int mod = obj.modulos(Convert.ToInt32(TextBox1.Text), Convert.ToInt32(TextBox2.Text));
Label1.Text = "Your Modulos Result =" + mod.ToString();
//you can use below script instead of Label control
//Response.Write("<script> alert(" + mod + ")</script>");
}
}

Step -11 Now Run the Application (webservice.aspx) --> Now Right Click Website in Solution

Explorer --> Select Build Website

Next Right Click Website in Solution Explorer --> Select View in Browser
Now Select the file webservice.aspx
Enter the Text Box Fields -->press each button( Add, Subtract......... ) to view the output.

Вам также может понравиться