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
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 asp.net 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.
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");
OleDbCommand cmd=new OleDbCommand();
public DataSet getEmp(int id)
OleDbDataAdapter da = new OleDbDataAdapter("select * from emp where empid=" + id,cn);
DataSet ds = new DataSet();
public bool checkEmp(int eno)
cmd.CommandText = "select count(*) from emp where empid=" + eno;
cmd.Connection = cn;
int i = (int)cmd.ExecuteScalar();
catch (Exception ex)
throw new Exception(ex.Message);
Asmx file of our empService with two webmethods is below
WSDL(Webservice Description Language) file of our empService is below.
Creating client application to consume our empService
-Select new asp.net 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.
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();
if (b == true)
System.Data.DataSet ds = obj.getEmp(int.Parse(TextBox1.Text));
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.
-If emp id doesn't exist out put will be as follows.
Thanks for reading the article.
Hope it is use full for beginners to understand how to create and consume a webservice in asp.net.
Feedbacks and suggestions are welcome.
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.
Public void mymethod()
"Hope for the best.. Prepare for the worst.."
Congratulations Shridhar your articles selected for 'Article of the day' in Microsoft official ASP.Net for March 01, 2016 - http://www.asp.net/community/articles
Am very much happy to see my article listed in the 'Article of the day' in Microsoft official ASP.Net for March 01, 2016.