Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

CRUD Operation in GridView Using WCF Service

Article:
Viewed:  7930 
Posted On:  20/02/2013 08:09:57 
In this article I am going to explain how to create, read, update and delete opration in asp.net uisng WCF 

In this article I am going to show CRUD (Create, Read, Update and Delete) operation in GridView using WCF service. For this my solution have 2 projects 1 for WCF and second is my asp.net applicaiton where I am consuming this WCF service. First I created a WCF project with name WCFService_CRUD.

Below is my IService1.cs

1.png

Image 1.

In this example I used a class Emp_Info as Data Contract. Here I defined some Data Member.

Emp_Info.cs will look like...

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Runtime.Serialization;
 
namespace WCFService_CRUD
{
    [DataContract]
    public class Emp_Info
    {
        int employeeNo;
        string name;
        string employeeCode;
        string keywords;
        string mobile;
        string address;
 
        [DataMember]
        public int EmployeeNo
        {
            get { return employeeNo; }
            set { employeeNo = value; }
        }
 
        [DataMember]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        [DataMember]
        public string EmployeeCode
        {
            get { return employeeCode; }
            set { employeeCode = value; }
        }
        [DataMember]
        public string Keywords
        {
            get { return keywords; }
            set { keywords = value; }
        }
        [DataMember]
        public string Mobile
        {
            get { return mobile; }
            set { mobile = value; }
        }
 
        [DataMember]
        public string Address
        {
            get { return address; }
            set { address = value; }
        }
    }
}
 
My Service1.svc.cs will look like as:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
 
namespace WCFService_CRUD
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
    public class Service1 : IService1
    {
        public DataSet ReadEmployee(Emp_Info empInfo)
        {
            SqlConnection con = new SqlConnection("server=.;database=Test;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM EMPLOYEE", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            cmd.ExecuteNonQuery();
            con.Close();
            return ds;
        }
 
        public bool DeleteEmployee(Emp_Info empInfo)
        {
            SqlConnection con = new SqlConnection("server=.;database=Test;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("DELETE FROM EMPLOYEE where EmployeeNo=" + empInfo.EmployeeNo, con);
            cmd.ExecuteNonQuery();
            con.Close();
            return true;
        }

        public void UpdateEmployee(Emp_Info empInfo)
        {
 
            SqlConnection con = new SqlConnection("server=.;database=Test;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("UPDATE EMPLOYEE set Name=@Name,Keywords=@Keywords,Mobile=@Mobile, Address=@Address where
EmployeeNo=@EmpNo"
, con);
            cmd.Parameters.AddWithValue("@EmpNo", empInfo.EmployeeNo);
            cmd.Parameters.AddWithValue("@Name", empInfo.Name);
            cmd.Parameters.AddWithValue("@Keywords", empInfo.Keywords);
            cmd.Parameters.AddWithValue("@Mobile", empInfo.Mobile);
            cmd.Parameters.AddWithValue("@Address", empInfo.Address);
            cmd.ExecuteNonQuery();
            con.Close();
        }
 
        public string CreateEmployee(Emp_Info empInfo)
        {
            string Message;
            SqlConnection con = new SqlConnection("server=.;database=Test;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO EMPLOYEE(EmployeeCode,Name,Keywords,Mobile,Address)
values(@EmpCode,@Name,@Keywords,@Mobile,@Address)"
, con);
            cmd.Parameters.AddWithValue("@EmpCode", empInfo.EmployeeCode);
            cmd.Parameters.AddWithValue("@Name", empInfo.Name);
            cmd.Parameters.AddWithValue("@Keywords", empInfo.Keywords);
            cmd.Parameters.AddWithValue("@Mobile", empInfo.Mobile);
            cmd.Parameters.AddWithValue("@Address", empInfo.Address);
            int result = cmd.ExecuteNonQuery();
            if (result == 1)
            {
                Message = empInfo.Name + " Record Inserted";
            }
            else
            {
                Message = empInfo.Name + " Insert Failed";
            }
            con.Close();
            return Message;
        }
    }
}
 
Now Compile WCF_CRUD project and run..

2.png

Image 2.

3.png

Image 3.

Now come to the application (CRUD Operation In GridView Using WCF Service).

Add WCF service reference here. Open Solution Explorer...

4.png

Image 4.

5.png

Image 5.

6.png

Image 6.

See Solution Explorer.

7.png
Image 7.

Open Default.aspx.cs page

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
 
public partial class _Default : System.Web.UI.Page
{
    ServiceReference1.Service1Client wcf_Obj = new ServiceReference1.Service1Client();
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            BindData();
    }
 
    private void BindData()
    {
        ServiceReference1.Emp_Info obj_Emp = new ServiceReference1.Emp_Info();
        DataSet ds = new DataSet();
        ds = wcf_Obj.ReadEmployee(obj_Emp);
        Gridview1.DataSource = ds;
        Gridview1.DataBind();
    }
 
    protected void Gridview1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        Gridview1.EditIndex = -1;
        BindData();
    }
 
    protected void Gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Gridview1.PageIndex = e.NewPageIndex;
        BindData();
    }
 
    protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int Employee_ID = Convert.ToInt32(Gridview1.DataKeys[e.RowIndex].Values["EmployeeNo"].ToString());
 
        ServiceReference1.Emp_Info obj_Emp = new ServiceReference1.Emp_Info();
        obj_Emp.EmployeeNo = Employee_ID;
        bool isDelete = wcf_Obj.DeleteEmployee(obj_Emp);
        if (isDelete)
        {
            BindData();
            lblresult.ForeColor = Color.Red;
            lblresult.Text = "Record deleted successfully";
        }
    }
 
    protected void Gridview1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Gridview1.EditIndex = e.NewEditIndex;
        BindData();
    }
 
    protected void Gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int Employee_ID = Convert.ToInt32(Gridview1.DataKeys[e.RowIndex].Values["EmployeeNo"].ToString());
        TextBox txtName = (TextBox)Gridview1.Rows[e.RowIndex].FindControl("txtName");       
        TextBox txtKeywords = (TextBox)Gridview1.Rows[e.RowIndex].FindControl("txtKeywords");
        TextBox txtMobile = (TextBox)Gridview1.Rows[e.RowIndex].FindControl("txtMobile");
        TextBox txtAddress = (TextBox)Gridview1.Rows[e.RowIndex].FindControl("txtAddress");
        ServiceReference1.Emp_Info obj_Emp = new ServiceReference1.Emp_Info();
        obj_Emp.Name = txtName.Text;
        obj_Emp.Keywords = txtKeywords.Text;
        obj_Emp.Mobile = txtMobile.Text;
        obj_Emp.EmployeeNo = Employee_ID;
        obj_Emp.Address = txtAddress.Text;
        wcf_Obj.UpdateEmployee(obj_Emp);
        BindData();
        lblresult.ForeColor = Color.Green;
        lblresult.Text = "Details Updated successfully";
    }

    protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            TextBox txtEmployee_Code = (TextBox)Gridview1.FooterRow.FindControl("txtEmployeeCode");
            
TextBox txtName = (TextBox)Gridview1.FooterRow.FindControl("txtName");
            TextBox txtKeywords = (TextBox)Gridview1.FooterRow.FindControl("txtKeywords");
            TextBox txtMobile = (TextBox)Gridview1.FooterRow.FindControl("txtMobile");
           
TextBox txtAddress = (TextBox)Gridview1.FooterRow.FindControl("txtAddress");
            ServiceReference1.Emp_Info obj_Emp = new ServiceReference1.Emp_Info();
            obj_Emp.EmployeeCode = txtEmployee_Code.Text;
            obj_Emp.Name = txtName.Text;
            obj_Emp.Keywords = txtKeywords.Text;
            obj_Emp.Mobile = txtMobile.Text;
            obj_Emp.Address = txtAddress.Text;
            string msg = wcf_Obj.CreateEmployee(obj_Emp);
            BindData();
            lblresult.ForeColor = Color.Green;

           
lblresult.Text = msg;
        }
    }
}
 
In yellow background you can see we are creating the object of WCF service and calling the method of our WCF service.

Below is my Data Table Structure..

14.png

Image 8.

When we will run the UI then the working will be...

8.png
Image 9.

To add a new record
9.png
Image 10.

Click on Add Button

10.png

Image 11.

To Update a Record..

11.png

Image 12.

To Delete A Record...

12.png

Image 13.

13.png

Image 14.

  Comment:
 
By karthik  On  24/07/2016 06:10:05
Hi, it is very useful to anyone.. can you please do the same thing by including (using) jquery ajax method which runs the code with out refreshing the whole page (jquery ajax + gridview + wcf)
 
By karthik  On  24/07/2016 06:09:26
Hi, it is very useful to anyone.. can you please do the same thing by including (using) jquery ajax method which runs the code with refreshing the whole page (jquery ajax + gridview + wcf)
 
By Satish  On  11/11/2014 05:27:20
Hi, Please update this code by providing the Default.aspx design code also. Then it will be more clear. If possible send me the code to my mail id lovesatti@gmail.com Thank you.
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us