In this article I will explain how to bind data in a GridView from SQL Server database using a WCF Service
Introduction
In this article first we will create a WCF service to get data from a SQL Server table. Then we will bind these data in a GridView control.
Step 1:
Create a new WCF Service Application
Image may be NSFW.
Clik here to view.
Note: I am using Microsoft Visual Web Developer 2010 Express
Step 2:
Add following code in IService1.cs Image may be NSFW.
Clik here to view.
Step 3:
Add following code in Service1.svc.cs
Image may be NSFW.
Clik here to view.
Note: Add your connection string in the Web.config file
Image may be NSFW.
Clik here to view.
Step 4:
Run the application to open WCF Test Client
Image may be NSFW.
Clik here to view.
Copy the path of Service1.svc
Step 5:
We have created a WCF service to retrieve data from database. Now we will create an ASP.NET Web Application and add drag a GridView control on Default.aspx as below
Image may be NSFW.
Clik here to view.
Step 6:
- Add WCF service reference in the project
- Right click on Service References in Solution Explorer
- Select Add Service Reference
- Paste location of Service1.svc in the Address box copied in Step 4
- Click on Go
- Click on OK
Image may be NSFW.
Clik here to view.
Step 7:
Add following code in Default.aspx.cs
Image may be NSFW.
Clik here to view.
Final Output: Image may be NSFW.
Clik here to view.
Filed under: .Net, ASP.NET, WCF Image may be NSFW.
Clik here to view.
