Creating And Consuming Web Services In ASP.NET

Introduction

In this article I am going to explain about Web Services, it is very much important and useful in communicating between the applications. 

The following example will detail about what is web service, creating and consuming the web service using C#, ASP.NET application.

What is Web Service

A web service is a web based functionality that we can use in different platforms using protocols. It is language independent, we can write web services in any language and access the same using any other language, the web service will pass the data using XML format. Web services communicates mainly by using http protocol.

Creating Web Service

In this example I am going to create a simple web service with the following details,
  • Create ASP.Net web service with Get All employee details method.
  • Web service method will get employee details from Database.
  • Create web application to access and bind the response details to grid.

Steps to create Web Service

  1. Create ASP.NET solution (WebServiceSolutionSample).
  2. Add an empty web application (WebServiceProjectSample) and add web service to this application.

    Add new project

    web service

  3. Create a web method to retrieve employee details from RKDB.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.Services;  
    6. using System.Data.SqlClient;  
    7. using System.Xml;  
    8. using System.Configuration;  
    9. using System.Data;  
    10. namespace WebServiceProjectSample  
    11. {  
    12.     /// <summary>  
    13.     /// Summary description for RKWebService  
    14.     /// </summary>   
    15.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
    16.     [System.ComponentModel.ToolboxItem(false)]  
    17.     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.   
    18.     // [System.Web.Script.Services.ScriptService]  
    19.     public class RKWebService: System.Web.Services.WebService  
    20.     {  
    21.         [WebMethod]  
    22.         public XmlElement GetAllEmployeesDetails()  
    23.         {  
    24.             SqlConnection conObj = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());  
    25.             conObj.Open();  
    26.             SqlCommand cmd = new SqlCommand("select * from Employee", conObj);  
    27.             cmd.ExecuteNonQuery();  
    28.             SqlDataAdapter daObj = new SqlDataAdapter(cmd);  
    29.             // Create an instance of DataSet.  
    30.             DataSet dsObj = new DataSet();  
    31.             daObj.Fill(dsObj);  
    32.             conObj.Close();  
    33.             // Return the DataSet as an XmlElement.  
    34.             XmlDataDocument xmlData = new XmlDataDocument(dsObj);  
    35.             XmlElement xmlElement = xmlData.DocumentElement;  
    36.             return xmlElement;  
    37.         }  
    38.     }  
    39. }  
    Config File
    1. <?xml version="1.0"?>  
    2.     <!--  
    3. For more information on how to configure your ASP.NET application, please visit  
    4. http://go.microsoft.com/fwlink/?LinkId=169433  
    5. -->  
    6.     <configuration>  
    7.         <system.web>  
    8.             <compilation debug="true" targetFramework="4.0" /> </system.web>  
    9.         <connectionStrings>  
    10.             <add name="dbconnection" connectionString="data source=localhost;Integrated Security=true;Initial Catalog=RKDB" /> </connectionStrings>  
    11.     </configuration>  
  4. Browse the web service and make sure that you are able to see the web method and its response.

    rkwebservice

    rkwebservice

Database Table Structure

table

Create a web application to access the employee details using web service.

Steps to create web application:

  1. Add new empty ASP.Net application (RKWebProject) to the solution (WebServiceSolutionSample).

    Application

  2. Add web service reference to this project (Add as a web reference).

    web service reference

    web service reference

  3. Create a Home.aspx page to access the web service.

    Home

    Home.aspx designer code
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="RKWebProject.Home" %>  
    2.     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    3.     <html xmlns="http://www.w3.org/1999/xhtml">  
    4.   
    5.     <head runat="server">  
    6.         <title></title>  
    7.     </head>  
    8.   
    9.     <body>  
    10.         <form id="form1" runat="server">  
    11.             <h1>WebService Sample</h1>  
    12.             <div>  
    13.                 <h2>Employee Details fetched using Asp.Net WebService</h2> </div>  
    14.             <div>  
    15.                 <asp:GridView ID="GVEmployeeDetails" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">  
    16.                     <AlternatingRowStyle BackColor="White" />  
    17.                     <EditRowStyle BackColor="#2461BF" />  
    18.                     <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
    19.                     <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
    20.                     <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />  
    21.                     <RowStyle BackColor="#EFF3FB" />  
    22.                     <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />  
    23.                     <SortedAscendingCellStyle BackColor="#F5F7FB" />  
    24.                     <SortedAscendingHeaderStyle BackColor="#6D95E1" />  
    25.                     <SortedDescendingCellStyle BackColor="#E9EBEF" />  
    26.                     <SortedDescendingHeaderStyle BackColor="#4870BE" /> </asp:GridView>  
    27.             </div>  
    28.         </form>  
    29.     </body>  
    30.   
    31.     </html>  
    Home.aspx Code File
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using System.Data;  
    8. using System.Xml;  
    9. namespace RKWebProject  
    10. {  
    11.     public partial class Home: System.Web.UI.Page  
    12.     {  
    13.         protected void Page_Load(object sender, EventArgs e)  
    14.         {  
    15.             if (!IsPostBack)  
    16.             {  
    17.                 BindEmployeeDetails();  
    18.             }  
    19.         }  
    20.         protected void BindEmployeeDetails()  
    21.         {  
    22.             RKWebServicePreject.RKWebService objRKWS = new RKWebServicePreject.RKWebService();  
    23.             DataSet dsResult = new DataSet();  
    24.             XmlElement exelement = objRKWS.GetAllEmployeesDetails();  
    25.             if (exelement != null)  
    26.             {  
    27.                 XmlNodeReader nodeReader = new XmlNodeReader(exelement);  
    28.                 dsResult.ReadXml(nodeReader, XmlReadMode.Auto);  
    29.                 GVEmployeeDetails.DataSource = dsResult;  
    30.                 GVEmployeeDetails.DataBind();  
    31.             }  
    32.         }  
    33.     }  
    34. }  
  4. Final output

    output


Similar Articles