Create A Web Service in Asp.Net With Employee Details

 

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.
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Services;
  7. using System.Data.SqlClient;
  8. using System.Xml;
  9. using System.Configuration;
  10. using System.Data;
  11. namespace WebServiceProjectSample
  12. {
  13. /// <summary>
  14. /// Summary description for SamWebService
  15. /// </summary>
  16. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  17. [System.ComponentModel.ToolboxItem(false)]
  18. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
  19. // [System.Web.Script.Services.ScriptService]
  20. public class SamWebService: System.Web.Services.WebService
  21. {
  22. [WebMethod]
  23. public XmlElement GetAllEmployeesDetails()
  24. {
  25. SqlConnection conObj = new SqlConnection(ConfigurationManager.ConnectionStrings[“dbconnection”].ToString());
  26. conObj.Open();
  27. SqlCommand cmd = new SqlCommand(“select * from Employee”, conObj);
  28. cmd.ExecuteNonQuery();
  29. SqlDataAdapter daObj = new SqlDataAdapter(cmd);
  30. // Create an instance of DataSet.
  31. DataSet dsObj = new DataSet();
  32. daObj.Fill(dsObj);
  33. conObj.Close();
  34. // Return the DataSet as an XmlElement.
  35. XmlDataDocument xmlData = new XmlDataDocument(dsObj);
  36. XmlElement xmlElement = xmlData.DocumentElement;
  37. return xmlElement;
  38. }
  39. }
  40. }

Config File

  1. <?xml version=”1.0"?>
  2. <configuration>
  3. <system.web>
  4. <compilation debug=”true” targetFramework=”3.5" /> </system.web>
  5. <connectionStrings>
  6. <add name=”dbconnection” connectionString=”data source=localhost;Integrated Security=true;Initial Catalog=sameer” /> </connectionStrings>
  7. </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. </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