create a web service in asp.net with Employee Details

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 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 communicate mainly by using the 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 getting All employee details method.
  • Web service method will get employee details from Database.
  • Create a web application to access and bind the response details to the grid.

Steps to create Web Service

Go to Visual Studio then click on “New” -> “Project” (WebServiceSolutionSample).




Add an empty web application (WebServiceProjectSample) and add web service to this application.



Go to Solution Explorer, then select the solution then click on “Add -> Add new item”




Choose the Web Service template.


Enter the name (for example SamWebService1) then click on “Add”.





  1. Create a web method to retrieve employee details from sameer.
    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 SamWebService  
    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 SamWebService: 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.     <configuration>  
    4.         <system.web>  
    5.             <compilation debug="true" targetFramework="3.5" /> </system.web>  
    6.         <connectionStrings>  
    7.             <add name="dbconnection" connectionString="data source=localhost;Integrated Security=true;Initial Catalog=sameer" /> </connectionStrings>  
    8.     </configuration>  





    Browse the web service and make sure that you are able to see the web method and its response.



    Now you will see Test checking.  Click on the “Invoke” button.




    Now you will see the result in an open standard form [XML].





    Database Table Structure




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

    Steps to create a web application:





    Add a new empty ASP.Net application (SamWebProject) to the solution (WebServiceSolutionSample).



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



    Click on “Advanced"





    Click on “Add  Web Reference"





    copy the URL




    Paste URL




    add web service 






    1. Create an AllEmployeesDetails.aspx page to access the web service.











    Home.aspx designer code
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AllEmployeesDetails.aspx.cs" Inherits="SamWebProject.AllEmployeesDetails" %>  
    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.     <head runat="server">  
    5.         <title></title>  
    6.     </head>  
    7.     <body>  
    8.         <form id="form1" runat="server">  
    9.             <h1>WebService Sample</h1>  
    10.             <div>  
    11.                 <h2>Employee Details fetched using Asp.Net WebService</h2> </div>  
    12.             <div>  
    13.                 <asp:GridView ID="GVEmployeeDetails" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">  
    14.                     <AlternatingRowStyle BackColor="White" />  
    15.                     <EditRowStyle BackColor="#2461BF" />  
    16.                     <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
    17.                     <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
    18.                     <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />  
    19.                     <RowStyle BackColor="#EFF3FB" />  
    20.                     <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />  
    21.                     </asp:GridView>  
    22.             </div>  
    23.         </form>  
    24.     </body>  
    25.   
    26.     </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 SamWebProject  
    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.           SamWebService.SamWebService1 objSamWS = new SamWebService.SamWebService1();
    23.             DataSet dsResult = new DataSet();  
    24.             XmlElement exelement = objSamWS.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. }  


    Final output


    Post a Comment

    0 Comments