We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Thursday, July 23, 2015

How to bind the records using web service and Asp .Net

In this article, we will see how to bind records on UI using web service in asp .net so now let us see step by step

Follow the given steps to do it. 
Step1:
  • Open the SQL Server Management Studio.
  • Create two tables (i)Employee Table and (ii)Department Table
Employee Table


Step1: Create a web service using the following steps.
  • Go to V.S 2010,2012,13 and take a New Project.
  • Select .NET Framework 3.5. and above
  • Take an ASP.NET Web Service Application.
  • Give it a name and click ok button.

Department Table


Step2: Create a web service using the following steps.
  • Go to V.S 2010,2012,13 and take a New Project.
  • Select .NET Framework 3.5. and above
  • Take a ASP.NET Web Service Application.
  • Give it a name and click ok button.

See the below


Step3:
By default, one service will create named Service.asmx and their code file named Service.cs placed in an application code folder i.e. App_Code, in which web method can be written. Even you can create new service also by right-clicking application root, select Add -> Add New Item -> Choose Web Service.

Now open your Web Service code file from App_Code folder to write the code to get the Employee details from the database as follows:

Service Name: EmployeeWebService.asmx

Code File (In App_Code Folder): EmployeeWebService.cs:


And  Write the code:EmployeeWebService.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for EmployeeWebService
/// </summary>
[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 EmployeeWebService : System.Web.Services.WebService {

    SqlConnection con = null;
    SqlDataAdapter da = null;
    DataSet ds = null;

    string strSql = string.Empty;
    public EmployeeWebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public DataSet GetEmployeeData() {

        con = new SqlConnection(@"Data Source=RASHMI;database=LabTask;User Id=sa;Password=123");

        strSql = "select e.EmpId,e.EmpName,e.EmpAddress,e.EmpSalary,e.EmpEmail,e.EmpPhone,e.EmpPine,e.DeptId,d.DName,d.Location from Employee e,Dept d Where e.DeptId=d.DeptId";
        da = new SqlDataAdapter(strSql, con);
        ds = new DataSet();
        da.Fill(ds, "Employee");
        return ds;
    }    
}

step4:
Run the program

see the output

and also click the GetEmployeeData

Step5: Test the client-side
Now create a web application to consume the service. For doing this, follow the given steps.

  • Go to VS and take a New Project.
  • Select an ASP.NET Empty Web Application.
  • Give it a name and click ok button.
Step 6: Now add service in our web application. Go to Solution Explorer and right-click at our project. Click at Add Web Reference. A new window will open. Its screen-shot is given below.

After select “Add Service Reference” option one window will open like this:



Now enter your locally deployed web service link and click Go button after that your web service will found and a window will look like this



Now click on OK button web service will add successfully that goes automatically in an application folder i.e. App_WebReferences as following:

Once the Service Reference is done, now open your Default.aspx page, and design like this


 Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
     <div align="center"> 
    <div align="center" style="width:880px;align-content:center">
        <h1  style="color:white;background-color:darkblue;border-radius:10px 10px 10px;border:double;box-shadow:10px 10px 10px maroon">Welcome to Employee Details</h1>
        <asp:GridView ID="gdv1" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" EnableModelValidation="True">
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
            <RowStyle ForeColor="#000066" />
            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
    
    </div></div>
    </form>
</body>
</html>


Code view
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 EmployeeServiceReference1;

public partial class _Default : System.Web.UI.Page
{
    EmployeeWebServiceSoapClient EWSSC = new EmployeeWebServiceSoapClient();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            DataSet ds = EWSSC.GetEmployeeData();
            gdv1.DataSource = ds.Tables["Employee"];
            gdv1.DataBind();
        }
    }
}

See the Final OutPut





No comments: