Search This Blog

Tuesday, January 10, 2012

web services in asp.net

How  to create a web service and Use of External Webservice.
add new website select webservice


aspx.cs File code

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

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

    public WebService () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod(Description="Addition of the numbers")]
    public int MethodToadd(int no1, int no2)
    {
        return no1 + no2;
    }
   
}
Where all web methods are there

Then debug first

then Add web Reference to and create webservices.

after that add wsdl will be genrated with disco file.

Add reference from d:/ documentandsetting /project/webservice.wsdl in the url
Then create new website and make one page default.aspx with two text box and add the numbers by clicking the button by using reference of ext webservices created before use that and use the webmethod of that.

Button1_click() code.

default.aspx.cs

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_Click(object sender, EventArgs e)
    {
        WebReference.WebService ser = new WebReference.WebService();
        int nn1 = int.Parse(n1.Text.ToString());
        int nn2 = int.Parse(n2.Text.ToString());
        int n;
        n=ser.MethodToadd(nn1,nn2);
        Label1.Text = "The Result is" + n.ToString();
        //Label1.Text = (ser.MethodToadd((int.Parse(n1.Text)),(int.Parse(n2.Text)))).ToString();
       
    }
}

No comments:

Post a Comment