WCF refers to Windows Communication Foundation and is a part of .NET 3.0 Framework, a product developed by Microsoft.
To get more details, visit my blog.
http://www.c-sharpcorner.com/blogs/key-notes-to-wcf
Description
We will be going through the following steps.
We will be going through the following steps.
- Create a WCF service.
- Using WCF service in your ASP.Net application.
- Bind a GridView using the WCF Service.
- CRUD operations on the GridView using the WCF service in ASP.NET.
- Textbox Validation using JavaScript.
Steps to be followed
Create two tables, as mentioned below.
Scripts
- CREATE TABLE [dbo].[Mas_Employee](
- [Id] [int] IDENTITY(1,1) NOT NULL,
- [Name] [varchar](50) NOT NULL,
- [Salary] [varchar](50) NOT NULL,
- [DeptId] [int] NOT NULL,
- [Status] [int] NULL,
- CONSTRAINT [PK_Mas_Employee] PRIMARY KEY CLUSTERED
- (
- [Id] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- CREATE TABLE [dbo].[Mas_Department](
- [DeptId] [int] IDENTITY(1,1) NOT NULL,
- [DeptName] [varchar](50) NOT NULL,
- [Status] [int] NULL,
- CONSTRAINT [PK_Mas_Department] PRIMARY KEY CLUSTERED
- (
- [DeptId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
Enter dummy data in Mas_Department table. That will be needed during inner join, with first table to fetch records.
- SET IDENTITY_INSERT [dbo].[Mas_Department] ON
- GO
- INSERT [dbo].[Mas_Department] ([DeptId], [DeptName], [Status]) VALUES (1, N'IT', 1)
- GO
- INSERT [dbo].[Mas_Department] ([DeptId], [DeptName], [Status]) VALUES (2, N'HR', 1)
- GO
- INSERT [dbo].[Mas_Department] ([DeptId], [DeptName], [Status]) VALUES (3, N'ACCOUNTS', 1)
- GO
- SET IDENTITY_INSERT [dbo].[Mas_Department] OFF
- GO
Create list of procedures to perform operations.
- IF EXISTS(SELECT NAME FROM sys.objects WHERE type = 'P' AND name = 'USP_Emp_Insert')
- DROP PROCEDURE USP_Emp_Insert
- GO
- -- =============================================
- -- Author:Satyaprakash Samantaray
- -- Opearion : To Insert emplyee details
- -- =============================================
- Create Procedure [dbo].[USP_Emp_Insert]
- @Name varchar(50),
- @Salary int,
- @DeptId int
- AS
- Begin
- Insert into Mas_Employee
- (Name,Salary,DeptId) Values
- (@Name,@Salary,@DeptId)
- End
- GO
- ----------------------------------------------------------------------------------------------------------------
- IF EXISTS(SELECT NAME FROM sys.objects WHERE type = 'P' AND name = 'USP_Emp_Update')
- DROP PROCEDURE USP_Emp_Update
- GO
- -- =============================================
- -- Author:Satyaprakash Samantaray
- -- Opearion : To update the emplyee details
- -- =============================================
- Create Procedure [dbo].[USP_Emp_Update]
- @Id int,
- @Name varchar(50),
- @Salary int,
- @DeptId int
- AS
- Begin
- update Mas_Employee Set
- Name=@Name,
- Salary=@Salary,
- DeptId=@DeptId
- where Id=@Id
- End
- GO
- ----------------------------------------------------------------------------------------------------------------
- IF EXISTS(SELECT NAME FROM sys.objects WHERE type = 'P' AND name = 'USP_Emp_Delete')
- DROP PROCEDURE USP_Emp_Delete
- GO
- -- =============================================
- -- Author:Satyaprakash Samantaray
- -- Opearion : To delete emplyee details
- -- =============================================
- Create Procedure [dbo].[USP_Emp_Delete]
- @Id int
- AS
- Begin
- Delete From Mas_Employee
- where Id=@Id
- End
- GO
- ----------------------------------------------------------------------------------------------------------------
- IF EXISTS(SELECT NAME FROM sys.objects WHERE type = 'P' AND name = 'Get_AllEmployees')
- DROP PROCEDURE Get_AllEmployees
- GO
- -- =============================================
- -- Author:Satyaprakash Samantaray
- -- Opearion : To get the emplyees details
- -- =============================================
- Create Procedure [dbo].[Get_AllEmployees]
- @Id int = null
- AS
- Begin
- Select E.Id, E.Name, E.Salary,E.DeptID,D.DeptName
- From Mas_Employee E
- Join Mas_Department D
- On E.DeptId = D.DeptId
- where D.Status = 1
- And Id = Isnull(@Id, Id)
- End
- GO
- ----------------------------------------------------------------------------------------------------------------
Step4
Create a WCF Service Application named "WCF_Crud".

Two files, IService1.cs and Service1.svc, will be added under the project in Solution Explorer.

Create a WCF Service Application named "WCF_Crud".

Two files, IService1.cs and Service1.svc, will be added under the project in Solution Explorer.

Step5
Then, in WEB.CONFIG file, add connection string and check some system generated codes.
Code Ref
Then, in WEB.CONFIG file, add connection string and check some system generated codes.
Code Ref
- <connectionStrings>
- <add name="conStr" connectionString="Put Connection string here...." providerName="System.Data.SqlClient"/>
- </connectionStrings>

Step6
In Iservices1.cs file, remove all the default code and declare the Service Contracts, Operation Contracts, and Data Contracts.
Code Ref
In Iservices1.cs file, remove all the default code and declare the Service Contracts, Operation Contracts, and Data Contracts.
Code Ref
- 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.Data;
- namespace WCF_Crud
- {
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- string InsertEmpDetails(EmpDetails eDatils);
- [OperationContract]
- DataSet GetEmpDetails(EmpDetails eDatils);
- [OperationContract]
- DataSet FetchUpdatedRecords(EmpDetails eDatils);
- [OperationContract]
- string UpdateEmpDetails(EmpDetails eDatils);
- [OperationContract]
- bool DeleteEmpDetails(EmpDetails eDatils);
- }
- [DataContract]
- public class EmpDetails
- {
- int? eId;
- string eName = string.Empty;
- string eSalary = string.Empty;
- string eDeptId = string.Empty;
- string eDeptName = string.Empty;
- [DataMember]
- public int? Id
- {
- get
- {
- return eId;
- }
- set
- {
- eId = value;
- }
- }
- [DataMember]
- public string Name
- {
- get
- {
- return eName;
- }
- set
- {
- eName = value;
- }
- }
- [DataMember]
- public string Salary
- {
- get
- {
- return eSalary;
- }
- set
- {
- eSalary = value;
- }
- }
- [DataMember]
- public string DeptId
- {
- get
- {
- return eDeptId;
- }
- set
- {
- eDeptId = value;
- }
- }
- [DataMember]
- public string DeptName
- {
- get
- {
- return eDeptName;
- }
- set
- {
- eDeptName = value;
- }
- }
- }
- }
Under [ServiceContract] attribute, I have defined some functions to perform operations using [OperationContract] attribute.
- [OperationContract]
- string InsertEmpDetails(EmpDetails eDatils);
- [OperationContract]
- DataSet GetEmpDetails(EmpDetails eDatils);
- [OperationContract]
- DataSet FetchUpdatedRecords(EmpDetails eDatils);
- [OperationContract]
- string UpdateEmpDetails(EmpDetails eDatils);
- [OperationContract]
- bool DeleteEmpDetails(EmpDetails eDatils);
- public class EmpDetails
- {
- int? eId;
- string eName = string.Empty;
- string eSalary = string.Empty;
- string eDeptId = string.Empty;
- string eDeptName = string.Empty;
- [DataMember]
- public int? Id
- {
- get
- {
- return eId;
- }
- set
- {
- eId = value;
- }
- }
- [DataMember]
- public string Name
- {
- get
- {
- return eName;
- }
- set
- {
- eName = value;
- }
- }
- [DataMember]
- public string Salary
- {
- get
- {
- return eSalary;
- }
- set
- {
- eSalary = value;
- }
- }
- [DataMember]
- public string DeptId
- {
- get
- {
- return eDeptId;
- }
- set
- {
- eDeptId = value;
- }
- }
- [DataMember]
- public string DeptName
- {
- get
- {
- return eDeptName;
- }
- set
- {
- eDeptName = value;
- }
- }
- }
- }
Now, open the Service.svc.cs file and add the below code. Also, define the methods declared in the IService1.cs above.
Code Ref
- 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.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- namespace WCF_Crud
- {
- // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
- public class Service1 : IService1
- {
- SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);
- public string InsertEmpDetails(EmpDetails eDetails) //For Insert Purpose
- {
- string Status;
- SqlCommand cmd = new SqlCommand("USP_Emp_Insert", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@Name", eDetails.Name);
- cmd.Parameters.AddWithValue("@Salary", eDetails.Salary);
- cmd.Parameters.AddWithValue("@DeptId", eDetails.DeptId);
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- int result = cmd.ExecuteNonQuery();
- if (result == 1)
- {
- Status = eDetails.Name + " " + eDetails.Salary + " Is Registered Successfully";
- }
- else
- {
- Status = eDetails.Name + " " + eDetails.Salary + " could not be registered";
- }
- con.Close();
- return Status;
- }
- public DataSet GetEmpDetails(EmpDetails eDetails) //For Details Purpose
- {
- SqlCommand cmd = new SqlCommand("Get_AllEmployees", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@Id", eDetails.Id);
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds);
- cmd.ExecuteNonQuery();
- con.Close();
- return ds;
- }
- public DataSet FetchUpdatedRecords(EmpDetails eDetails) //For update details Purpose
- {
- SqlCommand cmd = new SqlCommand("Get_AllEmployees", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@Id", eDetails.Id);
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds);
- cmd.ExecuteNonQuery();
- con.Close();
- return ds;
- }
- public string UpdateEmpDetails(EmpDetails eDetails) //For Update Purpose
- {
- string Status;
- SqlCommand cmd = new SqlCommand("USP_Emp_Update", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@Id", eDetails.Id);
- cmd.Parameters.AddWithValue("@Name", eDetails.Name);
- cmd.Parameters.AddWithValue("@Salary", eDetails.Salary);
- cmd.Parameters.AddWithValue("@DeptId", eDetails.DeptId);
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- int result = cmd.ExecuteNonQuery();
- if (result == 1)
- {
- Status = "Record Is Updated successfully";
- }
- else
- {
- Status = "Record could not be updated";
- }
- con.Close();
- return Status;
- }
- public bool DeleteEmpDetails(EmpDetails eDetails) //For Delete Purpose
- {
- SqlCommand cmd = new SqlCommand("USP_Emp_Delete", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@Id", eDetails.Id);
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- cmd.ExecuteNonQuery();
- con.Close();
- return true;
- }
- }
- }
Add the namespace lists.














Join the conversation! Your thoughts help the community grow.