Click here to Skip to main content
11,410,584 members (61,123 online)
Click here to Skip to main content

Insert, Update, Delete with Gridview ... Simple Way

, 4 Mar 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
This is a simple way to Insert, Update, Delete and Display through a single page.



This is a simple way to display, Update, Delete and Insert through a single page.

Using the Code

Many of us may be encountered with Update and Delete operations with GridView. I’m explaining this with more functionality that is Inserting record through the GridView. There could me more way to achieve this functionality but I think it’s a very simple way to achieve this. I’m taking a very simple table named "quest_categories"


CREATE TABLE [dbo].[quest_categories](
    [cat_id] [int] IDENTITY(1,1) NOT NULL,
    [cat_name] [varchar](150) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [cat_id] ASC

Here is the GridView that is going to perform the operation for Insert, Update and Delete the Data. Here I’ve added a Button "Add Category" for inserting a new record.


Here is the Code for "Add Category"

    protected void btnAdd_Click(object sender, EventArgs e)
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["exam_moduleConnectionString"].ConnectionString);
        SqlDataAdapter da = new SqlDataAdapter("SELECT cat_id, cat_name FROM quest_categories", con);
        DataTable dt = new DataTable();

        // Here we'll add a blank row to the returned DataTable
        DataRow dr = dt.NewRow();
        dt.Rows.InsertAt(dr, 0);
        //Creating the first row of GridView to be Editable
        GridView1.EditIndex = 0;
        GridView1.DataSource = dt;
        //Changing the Text for Inserting a New Record
        ((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text = "Insert";


After Clicking on "Add Category" the GridView will diplay like this. InsertingWithGridView/3.gif

And according to Text Diplays we’ll do further processing as "Update" or "Insert" record.

Here is the code for "RowUpdating" event.

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

        if (((LinkButton)GridView1.Rows[0].Cells[0].Controls[0]).Text == "Insert")
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["exam_moduleConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "INSERT INTO quest_categories(cat_name) VALUES(@cat_name)";
            cmd.Parameters.Add("@cat_name", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[0].Cells[2].Controls[0]).Text;

            cmd.Connection = con;
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["exam_moduleConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "UPDATE quest_categories SET cat_name=@cat_name WHERE cat_id=@cat_id";
            cmd.Parameters.Add("@cat_name", SqlDbType.VarChar).Value = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            cmd.Parameters.Add("@cat_id", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);
            cmd.Connection = con;

        GridView1.EditIndex = -1;

Rest operations are same as you might have done before so I’m not explaining it further.



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


About the Author

Web Developer TATA Communications
India India
No Biography provided

Comments and Discussions

QuestionMy vote is 3 PinmemberAshish Ramteke7-Jan-14 7:37 
GeneralColumn Type error Pinmemberrf_libra28-Jun-13 23:17 
Questionfgfdgdg PinmemberMember 85264792-Jun-13 9:50 
Suggestionnice article PinmemberAshutosh19625-Apr-13 20:19 
insert ,update ,delete in a gridview....the article is excellent but there is no store procedure is used in this.
how can we add store procedure to this application
ashutosh chaturvedi

QuestionMy vote of 5 PinmemberAlireza_136217-Apr-13 21:40 
GeneralMy vote of 3 Pinmembersupriya chaladi19-Mar-13 22:44 
Questionthank Pinmemberpusue15-Sep-12 1:22 
Questioncomment PinmemberMember 885114316-May-12 20:36 
Questioncshtml PinmemberMember 89490539-May-12 2:02 
QuestionNeed enhancement of form ? PinmemberMember 388846130-Apr-12 1:43 
QuestionInsert, Update, Delete with Gridview Pinmemberswarup2629-Apr-12 22:28 
Generalthanks...! Pinmembervinay_2897-Apr-12 2:23 
QuestionThank you! PinmemberMember 211582631-Jan-12 12:49 
GeneralMy vote of 5 Pinmemberhyderalima15-Sep-11 2:28 
GeneralMy vote of 5 PinmemberMonjurul Habib2-Mar-11 10:20 
GeneralMy vote of 5 Pinmemberdwyman17-Feb-11 12:48 
GeneralFor Select,Insert,Update and Delete in GridView without writting C# or Vb Code Pinmembervijay_vignesh26-Jan-11 2:24 
GeneralMy vote of 1 Pinmembersang15th525-Nov-10 6:59 
GeneralRow updation in a grid view [modified] Pinmemberrekha bothiraj15-Oct-10 5:22 
GeneralMy vote of 4 Pinmembervnkb5-Aug-10 21:27 
Generalthrown error Pinmemberkanimozhi.A1-Jun-10 21:04 
GeneralThank's a lot Pinmemberrudyswardani3-Apr-10 1:42 
GeneralI can't execute your example Pinmembertroy7118-Mar-10 16:32 
GeneralThaks A Lot PinmemberAbinash Bishoyi12-Jan-10 21:13 
QuestionMay i Know meaning of the Line Pinmembersakthipriya843-Dec-09 2:16 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.5 | Last Updated 4 Mar 2008
Article Copyright 2008 by rahul_bit
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid