1. Database structure

datab1

2. Table Data

datab2

 

3. Web form design Page code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewAddRecord.aspx.cs" Inherits="GridViewAddRecord" EnableEventValidation="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="false"  ShowFooter="true"  Width="500"  
         OnRowCommand="GridView1_RowCommand"  AutoGenerateEditButton="true"   AutoGenerateDeleteButton="true"  
         OnRowEditing="GridView_RowEditing" 
         OnRowCancelingEdit="GridView_RowCancelingEdit" 
         OnRowUpdating="GridView_RowUpdating"
         OnRowDeleting="GridView1_RowDeleting">
        <Columns>
                  <asp:TemplateField>
                    <HeaderTemplate> 
                    <asp:Label ID="HLabel1" runat="server" Text="Code"></asp:Label>
                    </HeaderTemplate>
                    
                    <ItemTemplate>
                     <asp:Label ID="Labelcid" runat="server" Text='<%#Eval("CID")%>'></asp:Label>
                     </ItemTemplate>
                    
                    
                    <FooterTemplate>
                       <asp:TextBox ID="txtcode" runat="server"></asp:TextBox>
                    </FooterTemplate>
                       
            </asp:TemplateField>
             <asp:TemplateField>
                    <HeaderTemplate> 
                    <asp:Label ID="HLabel1" runat="server" Text="Name"></asp:Label>
                    </HeaderTemplate>
                    
                    <ItemTemplate>
                     <asp:Label ID="Labelname" runat="server" Text='<%#Eval("name")%>'></asp:Label>
                     </ItemTemplate>
                    <EditItemTemplate>
                      <asp:TextBox ID="Edittxtname" runat="server" Text='<%#Eval("name")%>'></asp:TextBox>
                    </EditItemTemplate>

                    <FooterTemplate>
                       <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                    </FooterTemplate>
                       
            </asp:TemplateField>
            <asp:TemplateField>
                    <HeaderTemplate> 
                    <asp:Label ID="HLabel1" runat="server" Text="city"></asp:Label>
                    </HeaderTemplate>
                    
                    <ItemTemplate>
                     <asp:Label ID="Labelcity" runat="server" Text='<%#Eval("city")%>'></asp:Label>
                     </ItemTemplate>
                    
                    <FooterTemplate>
                       <asp:TextBox ID="txtcity" runat="server"></asp:TextBox>
                    </FooterTemplate>
                       
            </asp:TemplateField>
            <asp:TemplateField>
                    <HeaderTemplate> 
                    <asp:Label ID="HLabel1" runat="server" Text="Salary"></asp:Label>
                    </HeaderTemplate>
                    
                    <ItemTemplate>
                     <asp:Label ID="Labelsal" runat="server" Text='<%#Eval("sal")%>'></asp:Label>
                     </ItemTemplate>
                    
                    <FooterTemplate>
                       <asp:TextBox ID="txtsal" runat="server"></asp:TextBox>
                    </FooterTemplate>
                       
            </asp:TemplateField>

            <asp:TemplateField>
                <FooterTemplate>
                    <asp:Button ID="Button1" runat="server" Text="Add New" CommandName="AddNew" />
                </FooterTemplate>
            </asp:TemplateField>

       </Columns>
       </asp:GridView>
    </div>
    </form>
</body>
</html>

4. Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class GridViewAddRecord : System.Web.UI.Page
{  SqlConnection cnn = new SqlConnection();

    protected void DataToBind()
    {
      
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter ada = new SqlDataAdapter();
        DataSet ds = new DataSet();
        cnn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        cmd.CommandText = "select * from customer";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = cnn;

        ada.SelectCommand = cmd;
        ada.Fill(ds,"customer");
        GridView1.DataSource = ds;
        GridView1.DataBind();

        
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName.Equals("AddNew"))
        {

            try
            {

                TextBox txtcode = (TextBox)GridView1.FooterRow.FindControl("txtcode");
                TextBox txtcname = (TextBox)GridView1.FooterRow.FindControl("txtname");
                TextBox txtcity = (TextBox)GridView1.FooterRow.FindControl("txtcity");
                TextBox txtsal = (TextBox)GridView1.FooterRow.FindControl("txtsal");
                cnn = new SqlConnection();
                cnn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                cnn.Open();


                SqlCommand cmd = new SqlCommand("insert into customer(cid,name,city,sal) values(@cid,@name,@city,@sal)", cnn);

                cmd.Parameters.Add("cid", int.Parse(txtcode.Text));
                cmd.Parameters.Add("name", txtcname.Text);
                cmd.Parameters.Add("city", txtcity.Text);
                cmd.Parameters.Add("sal", double.Parse(txtsal.Text));

                int result = cmd.ExecuteNonQuery();
                cnn.Close();
                if (result == 1)
                {
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "codetpoint.com", "alert('Data Saved Sucessfully')", true);
                    DataToBind();
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "codetpoint.com", "alert('Not Saved ! error found')", true);
                }

            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "codetpoint.com", "alert('" + ex.Message + "')", true);
            }

        }
        
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataToBind();
        

        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        Label t1 = (Label)row.FindControl("Labelcid");


        bool t = DeleteData(t1.Text);
              if( t==true)
              {
                  Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "codetpoint.com", "alert('" + "Recor deleted"+ "')", true);
                  DataToBind();
              }


    }


    protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        DataToBind();
    }

    protected void GridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

        GridView1.EditIndex = -1;
        DataToBind();
        e.Cancel = true;
    }

    protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        
        GridViewRow row = GridView1.Rows[e.RowIndex];
        Label t1 = (Label)row.FindControl("Labelcid");
        TextBox t2 = (TextBox)row.FindControl("Edittxtname");

           

       //Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "codetpoint.com", "alert('" + t1.Text + t2.Text + "')", true);
        bool t = EditData( t1.Text, t2.Text);
        if (t == false)
            e.Cancel = true;

        Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "codetpoint.com", "alert('" +"Updated Sucessfully"+ "')", true);
        GridView1.EditIndex = -1;
        DataToBind();
    }

    bool EditData(String code, String Name)
    {
        bool flag = true;
        try
        {
            SqlConnection sqlcnn = new SqlConnection();
            sqlcnn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlCommand c = new SqlCommand();
            c.Connection = sqlcnn;
            c.CommandType = System.Data.CommandType.Text;
            c.CommandText = "update customer set name=@name where cid=@cid";
            c.Parameters.Add("@cid", SqlDbType.VarChar).Value = int.Parse(code);
            c.Parameters.Add("@name", SqlDbType.NVarChar, 100).Value =Name;
            
            sqlcnn.Open();
            c.ExecuteNonQuery();
            sqlcnn.Close();
        }
        catch (Exception ex)
        {
            flag = false;
        }
        return flag;
    
    }


    bool DeleteData(String code)
    {
        bool flag = true;
        try
        {
            SqlConnection sqlcnn = new SqlConnection();
            sqlcnn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlCommand c = new SqlCommand();
            c.Connection = sqlcnn;
            c.CommandType = System.Data.CommandType.Text;
            c.CommandText = "delete  from  customer  where cid=@cid";
            c.Parameters.Add("@cid", SqlDbType.VarChar).Value = int.Parse(code);
            

            sqlcnn.Open();
            c.ExecuteNonQuery();
            sqlcnn.Close();
        }
        catch (Exception ex)
        {
            flag = false;
        }
        return flag;
    
    }
}

5. web config

<?xml version="1.0" encoding="utf-8"?>

<configuration>
  
    <connectionStrings>
  <add name="DatabaseEntities" connectionString="metadata=res://*/App_Code.Model.csdl|res://*/App_Code.Model.ssdl|res://*/App_Code.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
 </connectionStrings>
 <system.web>
		<compilation debug="true" targetFramework="4.0"/>
	</system.web>
</configuration>

  

6. Run the application

gridview

 

7. Insert data & click add new button

gridinsert

 

After insert data

 gridview_after_insert

 

8. Update record Click edit link edit name field & click update link

gridupdate

 

 

 

9. After update check your database.

dbb11

 

10. Delete record click delete link

dellllle1 

11. Check your database after delete 

afdelll