Thursday, November 8, 2012

Consume Asp Web Service With C# Step By Step

My last post was about creating a asp.net web services,now its time to consume the web services by C# web or desktop application ,to consume asp.net web services by C# project simply add a web reference.
The web references url must be your web services location,then add the references.

After adding the references you will get the method to call,this must the web methods.


After adding the web references you will get this view .





Then the question is how to use this methods,the following simple code will help you to consume asp.net web services.


Sample Code :




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

          //WebServiceClient is the name of my project and localhost is the name what you named when add.


using WebServiceClient.localhost;

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

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
                             

               //ArithmaticOperation is the service name,where from I get the services.


            ArithmaticOperation ar = new ArithmaticOperation();
            if (DropDownList1.SelectedItem.Text.Equals("sumi"))
            {
                Label1.Text = "Resul = ";
                Label2.Text = ar.sums(TextBox1.Text, TextBox3.Text);
            }

            if (DropDownList1.SelectedItem.Text.Equals("muli"))
            {
                Label1.Text = "Resul = ";
                Label2.Text = ar.muls(TextBox1.Text, TextBox3.Text);
            }
            if (DropDownList1.SelectedItem.Text.Equals("divi"))
            {
                Label1.Text = "Resul = ";
                Label2.Text = ar.divs(TextBox1.Text, TextBox3.Text);
            }
            if (DropDownList1.SelectedItem.Text.Equals("mod"))
            {
                Label1.Text = "Resul = ";
                Label2.Text = ar.mod(Int32.Parse(TextBox1.Text), Int32.Parse(TextBox3.Text)).ToString();
            }
            if (DropDownList1.SelectedItem.Text.Equals("isnumber"))
            {
                Label1.Text = "Resul = ";
                if (ar.isnumber(TextBox1.Text))
                {
                    Label2.Text = "Yes This Is A Number";
                }
                else
                    Label2.Text = "No.This is not a number";
            
            }

            if (DropDownList1.SelectedItem.Text.Equals("validemail"))
            {
                Label1.Text = "Resul = ";
                if (ar.validemail(TextBox1.Text))
                {
                    Label2.Text = "This Email is Valid.";
                }
                else
                    Label2.Text = "This is not a valid email";

            }
            if (DropDownList1.SelectedItem.Text.Equals("sumarray"))
            {
                Label1.Text = "Resul = ";
                int []a={1,2,3,4,5,6,7,8,9,10};
                Label2.Text = ar.sumarray(a).ToString();

            }
        }
    }
}

Thats all,thanks.


No comments:

Post a Comment