Click here to Skip to main content
6,630,901 members and growing! (20,048 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate License: The Code Project Open License (CPOL)

Insert, Update, Delete with Gridview....simple way

By rahul_bit

This is a simple way to Insert, Update, Delete and Display through a single page.
C# (C# 2.0), .NET (.NET 2.0), ASP.NET, Dev
Posted:4 Mar 2008
Views:46,285
Bookmarked:23 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
20 votes for this article.
Popularity: 4.99 Rating: 3.83 out of 5
1 vote, 5.0%
1

2
3 votes, 15.0%
3
2 votes, 10.0%
4
14 votes, 70.0%
5

3.gif

Introduction

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


Table.gif

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,

CONSTRAINT [PK_quest_categories] PRIMARY KEY CLUSTERED

(

[cat_id] ASC

)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

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.

1.gif

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();
da.Fill(dt);

// 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;
GridView1.DataBind();
//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.
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;

con.Open();

cmd.ExecuteNonQuery();

con.Close();

}

else

{

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;

con.Open();

cmd.ExecuteNonQuery();

con.Close();

}

GridView1.EditIndex = -1;

BindData();

}

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

2.gif

Full Source code is available here : Download GridInsert - 2.99 KB

License

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

About the Author

rahul_bit


Member

Occupation: Web Developer
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
GeneralGreat PinmemberAmjad_pk19:32 27 Oct '09  
GeneralThe article is good Pinmemberswapniljakkulwar20:45 6 Aug '09  
GeneralThis is excellent solution Pinmemberftrghuy1:25 13 May '09  
GeneralHow do you handle cancel? Pinmemberdoran_doran12:28 23 Mar '09  
GeneralThanks! Pinmemberpap200513:35 19 Feb '09  
GeneralGood Article Pinmemberkjelltj4:33 26 Jan '09  
Generalgood one Pinmemberv.jayakrishnan9:06 10 Jan '09  
GeneralSome more resources on GridView here PinmemberSheo Narayan7:49 3 Jul '08  
Generalpoor article Pinmembermadhunangeth2:33 26 Jun '08  
GeneralGood Article PinmemberComputerAngel8:57 1 Jun '08  
GeneralIt doesn’t work correctly!!! Pinmemberdocsoft11:05 24 Mar '08  
GeneralBest Article PinmemberManish_Jaanvi3:12 4 Mar '08  
GeneralRe: Best Article PinmemberDewey14:35 4 Mar '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Mar 2008
Editor:
Copyright 2008 by rahul_bit
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project