In this Article we will learn how to create and consume Web Services In Asp.Net and C#.

A Web Service is platform independent software component, based on Simple Object Access Protocol (SOAP) .

Web Service is used to Exchange of data on the same of disparate system irrespective of architecture.

Web Services Communicate using Standard Web Protocol and data formats like HTTP, XML, SOAP

Now See how to create and Consume Web Services In Asp.Net.

Step 1- Create a New Website in Visual Studio.
Step 2- Right click on Project, Add New Item -> Select Web Service.

Now Open MyWebService.asmx.cs file and write the following code.

using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
namespace WebServicesDemo
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class MyWebService : System.Web.Services.WebService
    {
        [WebMethod]
        public DataSet GetAllEmployee()
        {
            string connectionStr=@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\AMIT\Documents\TestDB.mdf;Integrated Security=True;Connect Timeout=30";
            SqlConnection con = new SqlConnection(connectionStr);
            SqlCommand cmd = new SqlCommand("select * from Employee", con);
            con.Open();
            cmd.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            return ds;
        }
    }
}

In above code we are retrieving the Employee List from Employee table.

Step 3- Now Run your application.

You will A will be open , copy the url .

Step 4- Add a new WebForm.

Drag a grid view on It.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebServicesDemo.Index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView runat="server" ID="grdEmployee"></asp:GridView>
    </div>
    </form>
</body>
</html>

Step 5- Right Click On Project.

Select Add => Service Reference. A popup window will be open .

Paste that copied URL in Address bar, then click on GO.
Give any Name to your Service Reference.

Step 6- Go to code behind file.

Create a new Method Bind, which will be use to call the Services Method, and get the result.

Write the following code in code behind file

using System;
namespace WebServicesDemo
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                Bind();
            }
        }
        public void Bind()
        {
            MyServiceReference.MyWebServiceSoapClient objService = new MyServiceReference.MyWebServiceSoapClient();
            var getEmployee=objService.GetAllEmployee();
            grdEmployee.DataSource = getEmployee;
            grdEmployee.DataBind();
        }
    }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here