Search This Blog

Lets go..




Manage your Inventory | Grow your restaurant business | Manage your Employee | Control your accounts

Thursday

How to Create Web service and Consume web service in Web page


By: Mohammad Tuaha
How to Create Web service and Consume web service in Web page.

Or Create web service and consume web service

Requirement 
Visual Studio 2010 - up-to
 
Step 1: First create a website.
Step 2: Add new item web service file extension is .asmx

Step 3: In App_code Folder has à NewWebService_tut_1.cs file is the main web service operation file
Here is web service Method
  [WebMethod]
    public double Add(double x, double y)
    {
        return (x + y);
    }

    [WebMethod]
    public double Subtract(double x, double y)
    {
        return x - y;
    }

    [WebMethod]
    public double Multiply(double x, double y)
    {
        return x * y;
    }
It’s simple web service function. Now it’s ready to use Just build and RUN F5.
In my web service has two parameter one is X second is Y
Output is

How use it’s on web page
Step 4:
Open a new web project and add web reference.
Copy the web service url 
Add web Reference ànd paste Link into URL textbox


Step 5:
Now add two textbox and a button in Default.aspx page.

  protected void btnValueCalculation_Click(object sender, EventArgs e)
    {
        if (txtValue1.Text != string.Empty     && txtValue2.Text != string.Empty)
        {
            try
            {
                int x = Convert.ToInt32(txtValue1.Text);
                int y = Convert.ToInt32(txtValue2.Text);                               

              
                NewService.NewWebService_tut_1 service = new NewService.NewWebService_tut_1();
                lblAdd.Text = "Sum: " + service.Add(x, y).ToString();   
                lblSub.Text = "Difference: " + service.Subtract(x, y).ToString();
                lblmul.Text = "Multiplication: " + service.Multiply(x, y).ToString();
                lbldiv.Text = "Divide: " + service.Divide(x, y).ToString();
            }
            catch (Exception)
            {
                lblAdd.Text = "Please Fill Value ";
                  // Tuaha mohammad
            }
        }
    }

Now run your web project. User can use this web service any web platform any language like PHP ,jsp Asp 

Download Project

Next one is how to create web service with Database 



1 comment:

Thanks for your opinion