Click here to Skip to main content
14,659,434 members
Articles » Platforms, Frameworks & Libraries » Windows Communication Foundation » General
Posted 30 Sep 2012


107 bookmarked

WCF Example for Inserting, Deleting and Displaying Data using a WCF Service in ASP.NET

Rate this:
4.58 (79 votes)
Please Sign up or sign in to vote.
4.58 (79 votes)
11 Mar 2015CPOL
A practical example of a WCF service for inserting , Deleting and Displaying Data using a WCF Service in ASP.NET


WCF is more popular now a days and for beginners, I will show you a practical example of a WCF service for inserting, deleting and displaying data using ASP.NET.

Basically, this tip will demonstrate with example the following:

  • Step by step procedure/example to create WCF service
  • How to consume/access WCF service 
  • How to Use Proxy class
  • How to get data from WCF
  • How to bind/Load/Fill gridview from SQL Server database using WCF service
  • How to perform insert, edit, update and delete operation on gridview using WCF service

Using the Code

For inserting data into a database using a WCF service in ASP.NET, we have to do the following steps:

  • Create a WCF service
  • Create a Web based application

Part 1: Create a WCF Service

Open Visual Studio 2010/2012.

Step 1: Open Visual Studio -> Go to File menu -> New -> Project -->  Select the new Empty Solution and give name WCFProject.

Image 1

Step 2: Then add new Class Library Project and give name SaleClassLibrary.

Step 3: Then add reference of System.ServiceModel and System.Runtime.Serialization into that project.

Step 4: Then add new Interface ISaleService.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;

    interface ISaleService
         bool InsertCustomer(Customer obj);

        List<Customer> GetAllCustomer();

        bool DeleteCustomer(int Cid);

        bool UpdateCustomer(Customer obj);     

   public class Customer
        public int CustomerID;
        public string CustomerName;        
        public string Address;
        public string EmailId;

Step 5: And write the following code in the SaleService.cs file:

SaleService.cs page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public class SaleService : ISaleService
        public bool InsertCustomer(Customer obj)
            return true;

        public List<Customer> GetAllCustomer()
            return cutomerList;

        public bool DeleteCustomer(int Cid)
            var item = cutomerList.First(x => x.CustomerID == Cid);

            return true;

        public bool UpdateCustomer(Customer obj)
            var list = cutomerList;
            cutomerList.Where(p => p.CustomerID == 
            obj.CustomerID).Update(p => p.CustomerName = obj.CustomerName);
            return true;

      public static  List<Customer> cutomerList = new List<Customer>()
        new Customer {CustomerID = 1, CustomerName="Sujeet", 
        Address="Pune", EmailId="" },
        new Customer {CustomerID = 2, CustomerName="Rahul", 
        Address="Pune", EmailId="" },
        new Customer {CustomerID = 3, CustomerName="Mayur", 
        Address="Pune", EmailId=""}
 public static class LinqUpdates
        public static void Update<T>(this IEnumerable<T> source, Action<T> action)
            foreach (var item in source)

Step 6: Build your Class library.

Step 7: Add New Empty ASP.NET Project into that Solution, give name SaleServiceHost.

Image 2

Step 8: Add reference of Classlibrary Project to that SaleServiceHost ASP.NET project.

Step 9: Add new item WCF Service in that SaleServiceHost ASP.NET project and give name SaleService.

Image 3

Step 10: Right click on that SaleService.svc and select view Markup:

Image 4

Change the Service Name from that markup:

<%@ ServiceHost Language="C#" Debug="true" 

Service="SaleClassLibrary.SaleService"  %>

Step 11: And build the solution and SaleServiceHost set as startup project and SaleService.svc set as startup page.

Image 5

In this way, your WCF service builds successfully.

Part 2: Create a Web Based Application (Client)

Now, create your client application in your system:

Image 6

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        Welcome to Sale Service</h2><table class="style1">
                <td style="text-align: right">
                    Enter name</td>
                    <asp:TextBox ID="TextBox1" 

                <td class="style2">
                    <asp:TextBox ID="TextBox2" 

                <td class="style2">
                    Email ID</td>
                    <asp:TextBox ID="TextBox3" 

                <td class="style2">
                    <asp:Button ID="Button1" runat="server" 

                    onclick="Button1_Click" Text="Save" />
        <asp:GridView ID="GridView1" runat="server" 

        AllowPaging="True"  DataKeyNames="CustomerID,CustomerName"

            AllowSorting="True" AutoGenerateDeleteButton="True" 

        <asp:Label ID="Label1" runat="server" 

using SaleService;
  1. Create a Website.
  2. Add Service Reference http://localhost:53544/SaleService.svc?wsdl to a Web Application.
  3. Select your Website.
  4. Right click on it, add Service Reference, then enter your Service URL and click Go.
  5. Give the name for your service SaleService -> OK.
  6. Then automatically a proxy will be created in your client system.
  7. Write the following code in your source code:
  8. Add your service reference on the top.
  9. Then create an object for Service Reference and use that object to call the methods from your service.
  10. Write the following code in your aspx.cs file.
Default.aspx.cs page:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using ServiceReference1;

public partial class _Default : System.Web.UI.Page
    SaleService.SaleServiceClient proxy;
        protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)
                 proxy=new SaleService.SaleServiceClient();

        protected void Button1_Click(object sender, EventArgs e)
            proxy = new SaleService.SaleServiceClient();
            SaleService.Customer objcust = 
            new SaleService.Customer() { CustomerID=5, CustomerName=TextBox1.Text,
            Address=TextBox2.Text,EmailId=TextBox3.Text  };


            GridView1.DataSource = proxy.GetAllCustomer();
            Label1.Text = "Record Saved Successfully";

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
            int userid = Convert.ToInt32(GridView1.DataKeys
            proxy = new SaleService.SaleServiceClient();

            bool check = proxy.DeleteCustomer(userid);
              Label1.Text = "Record Deleted Successfully";
              GridView1.DataSource = proxy.GetAllCustomer();

By using this, you have successfully inserted data in the database and you have also shown this in the gridview.

Image 7

Please take a look at the attached code for more information.

Happy programming!!

Don’t forget to leave your feedback and comments below!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sujeet Bhujbal
Technical Lead
India India
Sujit Bhujbal is Senior Software Engineer having over 6 + years of Experience in 3.5,WPF,WCF and has worked on various platforms. He worked during various phases of SDLC such as Requirements and Analysis, Design and Construction, development, maintenance, Testing, UAT.

He is Microsoft Certified Technology Specialist (MCTS) in /WCF Applications. He worked at various levels and currently working as a Senior Software Engineer.

His core areas of skill are Web application development using WPF,WCF , C#.Net, ASP.Net 3.5, WCF, SQL Server 2008, CSS, Java script Web design using HTML, AJAX and Crystal Reports

He is proficient in developing applications using SilverLight irrespective of the language, with sound knowledge of Microsoft Expression Blend and exposure to other Microsoft Expression studio tools.

Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

Blog: Visit Sujit Bhujbal

CodeProject:- Sujit Bhujbal codeproject

DotNetHeaven:- DotNetHeaven


Linkedin :-Linkedin

Stack-Exchange: <a href="

Comments and Discussions

QuestionOne of the most confusing tutorials I've ever tried to follow Pin
Member 1274969820-Sep-16 0:48
MemberMember 1274969820-Sep-16 0:48 
AnswerRe: One of the most confusing tutorials I've ever tried to follow Pin
Member 1274969820-Sep-16 1:04
MemberMember 1274969820-Sep-16 1:04 
GeneralRe: One of the most confusing tutorials I've ever tried to follow Pin
Sujeet Bhujbal16-Mar-17 19:39
MemberSujeet Bhujbal16-Mar-17 19:39 
QuestionDB Connection Pin
svassilianski28-Jan-16 9:44
Membersvassilianski28-Jan-16 9:44 
GeneralMy vote of 4 Pin
Member 1206058129-Oct-15 21:07
MemberMember 1206058129-Oct-15 21:07 
GeneralMy Vote Of 5 Pin
Sanket Bankar28-Oct-15 1:13
MemberSanket Bankar28-Oct-15 1:13 
GeneralRe: My Vote Of 5 Pin
Sujeet Bhujbal28-Oct-15 1:15
MemberSujeet Bhujbal28-Oct-15 1:15 
QuestionThere was no endpoint listening at http://localhost:53544/SaleService.svc that could accept the message Pin
Member 1207934022-Oct-15 5:50
MemberMember 1207934022-Oct-15 5:50 
AnswerRe: There was no endpoint listening at http://localhost:53544/SaleService.svc that could accept the message Pin
Sujeet Bhujbal27-Oct-15 3:47
MemberSujeet Bhujbal27-Oct-15 3:47 
GeneralMy vote of 5 Pin
Santhakumar Munuswamy @ Chennai8-Aug-15 4:43
professionalSanthakumar Munuswamy @ Chennai8-Aug-15 4:43 
GeneralRe: My vote of 5 Pin
Sujeet Bhujbal27-Oct-15 3:43
MemberSujeet Bhujbal27-Oct-15 3:43 
GeneralMy Vote of 5 Pin
Math08avan12-Jul-15 0:39
MemberMath08avan12-Jul-15 0:39 
GeneralRe: My Vote of 5 Pin
Sujeet Bhujbal27-Oct-15 3:42
MemberSujeet Bhujbal27-Oct-15 3:42 
QuestionThank you Pin
Member 1168587712-May-15 15:27
MemberMember 1168587712-May-15 15:27 
AnswerRe: Thank you Pin
Sujeet Bhujbal18-Jun-15 20:02
MemberSujeet Bhujbal18-Jun-15 20:02 
QuestionThanks Sujit Bhujbal Pin
Nguyên Hoàng Nguyễn Đức7-May-15 23:00
professionalNguyên Hoàng Nguyễn Đức7-May-15 23:00 
AnswerRe: Thanks Sujit Bhujbal Pin
Sujeet Bhujbal18-Jun-15 20:03
MemberSujeet Bhujbal18-Jun-15 20:03 
QuestionGetting error in VS2012 Pin
JomyK7-May-15 13:15
MemberJomyK7-May-15 13:15 
AnswerRe: Getting error in VS2012 Pin
Sujeet Bhujbal18-Jun-15 20:18
MemberSujeet Bhujbal18-Jun-15 20:18 
GeneralCongrats Pin
Member 111054606-Apr-15 22:43
MemberMember 111054606-Apr-15 22:43 
GeneralRe: Congrats Pin
Sujeet Bhujbal18-Jun-15 20:09
MemberSujeet Bhujbal18-Jun-15 20:09 
QuestionAdnan Pin
Member 1151696711-Mar-15 7:13
MemberMember 1151696711-Mar-15 7:13 
AnswerRe: Adnan Pin
Sujeet Bhujbal15-Mar-15 19:41
MemberSujeet Bhujbal15-Mar-15 19:41 

Please let me know which bug you found
Sujeet Bhujbal

Please visit my blog :

QuestionLink not working Pin
Humayun Kabir Mamun10-Mar-15 20:22
MemberHumayun Kabir Mamun10-Mar-15 20:22 
AnswerRe: Link not working Pin
Sujeet Bhujbal11-Mar-15 0:09
MemberSujeet Bhujbal11-Mar-15 0:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.