Bind GridView using data from WCF Service

Bind GridView using data from WCF Service. By using web services binding gridview is very simple with just two line of code.

Create a table names Employee with Emp id,Name and Country. Please make use of the below query to create Employee table.

CREATE TABLE [dbo].[Employee](
  [EmpID] [int] NOT NULL,
  [Name] [varchar](50) NOT NULL,
  [Country] [varchar](50) NOT NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[Employee] ([EmpID], [Name], [Country]) VALUES (1, N'Kiruba', N'India')
GO
INSERT [dbo].[Employee] ([EmpID], [Name], [Country]) VALUES (2, N'Geetha', N'India')
GO
INSERT [dbo].[Employee] ([EmpID], [Name], [Country]) VALUES (3, N'Jimmy', N'France')
GO
INSERT [dbo].[Employee] ([EmpID], [Name], [Country]) VALUES (4, N'Kalyani', N'Russia')
GO

Add new webservices and name it as MyService. Add below code in IMyService. file.
DataContract class named EmployeeData which contains a DataTable Property EmployeeTable which will be used to send the data from the WCF Service to the Web Application.
The IMyService Interface has a method GetEmployee which is decorated with OperationContract attribute.

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    EmployeeData GetEmployee();
}
 
[DataContract]
public class EmployeeData
{
    public EmployeeData()
    {
        this.EmployeeTable = new DataTable("EmployeeData");
    }
 
    [DataMember]
    public DataTable EmployeeTable { get; set; }
}

 

Add below code in MyService.svc file.

public class MyService : IMyService
{
    public EmployeeData GetEmployee()
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT Empid, Name, Country FROM Employee"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        EmployeeData employee= new EmployeeData();
                        sda.Fill(employee.EmployeeTable);
                        return employee;
                    }
                }
            }
        }
    }
}

Build the service and host in IIS server. Then add the reference to the service from the web application.

Add below code in aspx page.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
    <asp:BoundField DataField="Empid" HeaderText="EmpId" ItemStyle-Width="150" />
    <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
    <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100" />
</Columns>
</asp:GridView>

Bind the grid control using the method in MyService on page load as shown below.

protected void Page_Load(object sender, EventArgs e)
{
    ServiceReference.MyService services = new ServiceReference.MyService();
    GridView1.DataSource = services .GetEmployee().EmployeeTable;
    GridView1.DataBind();
}

 

Output

Output1