How to create and consume webservice

In this article am going to explain how to create a webservice using visual studio 2010 and how to consume the created webservice with in the client application. There might be some other articles too, but I explained clearly step by step which will be use full for beginners to understand easily.

In this article we are going to learn how to create a webservice which has two webmethods and how to consume created service with client application.


Webservice is a class managed by the webserver, which is accessible to client application based on
following standards.
1.WSDL(Webservice Description Language)
2.SOAP(Simple Object Access Protocol)
3.HTTP(Hyper Text Transfer Protocol)
4.UDDI(Universal Description Discovery Intergration)

We are going to use below two methods in our empService


This method use to load emp details from access database and stores in the dataset.

This method is used to verify whether the employee exists in the database.
If employee was found then returns the employee record else displays employee does not exist.

Go to access database and create emp table with following records

Empid Ename Egender Esal Edep
101 ramu male 2500 1
102 raju male 4500 2
103 rani female 5000 2
104 sridhar male 6000 3

Creating webservice using visual studio 2010

-Select new empty wesite and name as Empwebservice.
-Right click on website path and select add new item.
-Secelct webservice from list and name as empService.
-Visual studio creates asmx and cs files.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.OleDb;
using System.Data;
public class empService : System.Web.Services.WebService
OleDbConnection cn=new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E://srid documents/mydocsssssss/products.accdb;User ID=admin");
cmd=new OleDbCommand();
DataSet getEmp(int id)
da = new OleDbDataAdapter("select * from emp where empid=" + id,cn);
DataSet ds = new DataSet();
return ds;
public bool checkEmp(int eno)

cmd.CommandText = "select count(*) from emp where empid=" + eno;
cmd.Connection = cn;
int i = (int)cmd.ExecuteScalar();
if (i==1)
return true
return false
(Exception ex)
new Exception(ex.Message);


Asmx file of our empService with two webmethods is below

empService asmx

WSDL(Webservice Description Language) file of our empService is below.

empService wsdl

Creating client application to consume our empService

-Select new website, name as clientEmp.
-Add new item, select default.aspx web form.
-Drag and drop gridview control, textbox control and button control.
-Go to solution explorer right click and add webreference.
-Adding reference will create proxy class to communicate with service.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.WebControls;
public partial class
_Default : System.Web.UI.Page
protected void
Page_Load(object sender, EventArgs e)

protected void
Button1_Click(object sender, EventArgs e)
localhost.empService obj=new localhost.empService();
(b == true)
System.Data.DataSet ds = obj.getEmp(int.Parse(TextBox1.Text));
GridView1.DataSource= ds.Tables[0];
GridView1.Visible = true;

Response.Write("Emp id "+TextBox1.Text+" Doesnot exist");
GridView1.Visible = false;

Run the empService first then run the client application

-If you run directly client application with out running server, you will get following error.
-So make sure that server is running before you run client application. Now press F5 to run client application.
-Enter emp id and click search.

out put

-If emp id doesn't exist out put will be as follows.

error out put

Thanks for reading the article.
Hope it is use full for beginners to understand how to create and consume a webservice in
Feedbacks and suggestions are welcome.


Author: Sridhar Thota26 May 2015 Member Level: Gold   Points : 7

Web service description language is a xml based language for providing complete description of webservice to client application.
Description contains which data types used, method names and address in webservice.

Simple object access protocol is the formatting protocol which provides standards for sending request and receiving response between client and webservice.

Hyper text transfer protocol is used as a transport to carry request in form of soap message from client to webserver and response from webserver to client.

Universal description discovery and integration is used for describing the services and is platform independent.

Mechanism of HTTP, SOAP in webservice:
1.Client application method call will go to proxy,
proxy will convert method call to xml format that
is soap message.
2.Soap message will reach webserver via HTTP
3.Webserver will create object of webservice and
method is executed.
4.The result in the form of soap format is given to
client back over HTTP.

The methods present in webservice which are to be exposed to client application are attributed with webmethod.
Ex: [webmethod]
Public void mymethod()
//some logic.


DNS Member.
"Hope for the best.. Prepare for the worst.."

Author: Gaurav Aroraa01 Mar 2016 Member Level: Gold   Points : 0

Congratulations Shridhar your articles selected for 'Article of the day' in Microsoft official ASP.Net for March 01, 2016 -

Author: Sridhar Thota01 Mar 2016 Member Level: Gold   Points : 0

Thanks Gaurav...!
Am very much happy to see my article listed in the 'Article of the day' in Microsoft official ASP.Net for March 01, 2016.

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: