Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Web Service In ASP.NET C-Sharp

Article:
Viewed:  1180 
Posted On:  13/02/2015 20:58:25 
What is Web Service in asp.net c#? How we can create and consume a Web Service in asp.net c# ? 

In this article I am going to explain What is Web Service? How we can create and consume a Web Service?

What is Web Service?

Web services are components on a Web server that a client application can call by making HTTP requests across the Web. ASP.NET enables you to create custom Web services or to use built-in application services, and to call these services from any client application. 

Web Services communicate by using standard web protocols and data formats ie:

HTTP
XML
SOAP

Some term usually we see while using Web Services:

SOAP: Simple Object Access Protocol. SOAP is way by which method calls are translate into XML format and sent via HTTP.

WSDL: Web Service Description Language. WSDL contains every detail regarding using web service and Method and Properties provided by web service and URLs from which those methods can be accessed and Data Types used.

UDDI: Universal Description, Discovery and Integration (UDDI) is a directory service where businesses can register and search for Web services.

Discovery or .Disco Files: The DISCO file typically points to a WSDL source that in turn points to the actual Web Service. When one searches for the Web Services.

Now we will see how we can create and consume Web Services in asp.net c#. Below is my Data Table in Design mode from which I will fetch data using web service:

1.png

Image 1.

Script of my Data Table is:

CREATE TABLE [dbo].[Employee](
          [Emp_ID] [int] IDENTITY(1,1) NOT NULL,
          [Name] [varchar](50) NULL,
          [Designation] [varchar](50) NULL,
          [City] [varchar](50) NULL,
          [State] [varchar](50) NULL,
          [Country] [varchar](50) NULL,
   CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
  (
          [Emp_ID] ASC
  )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF,
  IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON
    PRIMARY]
  ) ON [PRIMARY]
  GO

Data in My Table is:

2.png

Image 2.

Now open Visual Studio -> Select File -> New Web Site-> ASP.NET Web Service.

3.png

Image 3.

Now open Service.cs inside App_Code folder to write your WebMethod like below:

4.png

Image 4.

App_Code/Service.cs is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using System.Xml;
 
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    public Service()
    {
        //Uncomment the following line if using designed components
        //InitializeComponent();
    }
 

    [WebMethod]
    public XmlElement GetEmployeeSearchResult(string EMP_Name)
    {
        using (SqlConnection con = new SqlConnection(@"Data Source=INDIA\MSSQLServer2k8; Initial Catalog=EmployeeManagement; Uid=sa; pwd=india;"))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM EMPLOYEE WHERE Name like @EMP_Name+'%'", con))
            {
                con.Open();
                cmd.Parameters.AddWithValue("@EMP_Name", EMP_Name);
                cmd.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
               
                DataSet ds = new DataSet();
                da.Fill(ds);
                con.Close();
                
                XmlDataDocument xmldata = new XmlDataDocument(ds);
                XmlElement xmlElement = xmldata.DocumentElement;
                return xmlElement;
            }
        }
    }
}

Now time to check your Web Service So Run your application:

5.png

Image 5.

Now click on your Method name:

6.png

Image 6.

7.png

Image 7.

Now time to consume this web service so create a new ASP.NET application

8.png

Image 8.

Now Right click on your application Solution Explorer -> Add Web Reference

9.png

Image 9.

Type your web service URL and click GO. After it Click on Add Reference Button:

10.png

Image 10.

Now my aspx is:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Web Services In ASP.NET C#</title>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <table style="border: solid 15px blue; width: 100%; vertical-align: central;">
            <tr>
                <td style="padding-left: 20px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue;
                    text-align: center; font-family: Verdana; font-size: 20pt; color: red;">
                    Web Services In ASP.NET C#
                </td>
            </tr>
            <tr>
                <td style="background-color: skyblue; text-align: center; font-family: Verdana; font-size: 14pt;
                    color: red;">
                    <b>Enter Employee Name:</b>
                    <asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
                    <asp:Button ID="btnSearchEmployee" runat="server" Text="Search Employee" OnClick="btnSearchEmployee_Click" />
                </td>
            </tr>
            <tr>
                <td>
                    <table style="width: 80%; text-align: center; vertical-align: central;">
                        <tr>
                            <td style="text-align: left;">
                                <asp:GridView ID="GridViewEmployee" runat="server" AutoGenerateColumns="False" Width="100%"
                                    BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
                                    CellPadding="4">
                                    <Columns>
                                        <asp:BoundField DataField="Name" HeaderText="Employee Name" HeaderStyle-HorizontalAlign="Left">
                                            <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                                        </asp:BoundField>
                                        <asp:BoundField DataField="Designation" HeaderText="Designation" HeaderStyle-HorizontalAlign="Left">
                                            <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                                        </asp:BoundField>
                                        <asp:BoundField DataField="City" HeaderText="City" HeaderStyle-HorizontalAlign="Left">
                                            <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                                        </asp:BoundField>
                                        <asp:BoundField DataField="State" HeaderText="State" HeaderStyle-HorizontalAlign="Left">
                                            <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                                        </asp:BoundField>
                                        <asp:BoundField DataField="Country" HeaderText="Country" HeaderStyle-HorizontalAlign="Left">
                                            <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                                        </asp:BoundField>
                                    </Columns>
                                    <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                                    <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
                                    <PagerStyle ForeColor="#003399" HorizontalAlign="Left" BackColor="#99CCCC" />
                                    <RowStyle BackColor="White" ForeColor="#003399" />
                                    <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                                </asp:GridView>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
 

My aspx.cs is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindEmployee("");
        }
    }
 
    protected void BindEmployee(string Emp_Name)
    {
        localhost.Service obj = new localhost.Service();
        DataSet ds = new DataSet();
        XmlElement exElement = obj.GetEmployeeSearchResult(Emp_Name);
        if (exElement != null)
        {
            XmlNodeReader nodeReader = new XmlNodeReader(exElement);
            ds.ReadXml(nodeReader, XmlReadMode.Auto);
            GridViewEmployee.DataSource = ds;
            GridViewEmployee.DataBind();
        }
        else
        {
            GridViewEmployee.DataSource = null;
            GridViewEmployee.DataBind();
        }
    }

    protected void btnSearchEmployee_Click(object sender, EventArgs e)
    {
        BindEmployee(txtEmpName.Text);
    }
}


Now run Your application:

11.png

Image 11.

Now enter some name and click on Search

12.png

Image 12.

13.png

Image 13.

14.png

Image 14.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us