Click here to Skip to main content
12,818,785 members (29,208 online)

How to edit, update and delete in gridview when data comes from database?

amitesh1989 asked:

Open original thread
This question can be ask here many time but my case is different. Please forgive me if i am wrong i have a page in which i have text-box and a button when i enter department name in text-box and press submit button my data store in the database

Now what i want is i want
(1) When i press submit button i can see that department in my grid-view.
(2) to show my department name in grid with edit and delete link so that when i click on edit link department name will pop in the department text-box and submit button become update button and when i make some changes and click on update button it will update and show it in the grid.

how can i do that This is my department page.cs code:
public partial class DepartmentMaster : System.Web.UI.Page
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["WebGallery"].ToString());
SqlDataAdapter da; DataTable dt = new DataTable();
 DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
da = new SqlDataAdapter("Select * from Department_Master", con); da.Fill(ds);
gvDepartment.DataSource = ds;
 protected void btnSubmit_Click(object sender, EventArgs e)
 { using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyGallery"].ToString()))
con.Open(); string DepartmentName = tbDepartment.Text.Trim();
 using (SqlCommand cmd = con.CreateCommand())
cmd.CommandText = "insert into Department_Master(DepartmentName) values('" + DepartmentName + "')";
cmd.Parameters.AddWithValue("@DepartmentName", tbDepartment.Text.Trim());
tbDepartment.Text = "";
Tags: C#, ASP.NET, Gridview, buttons, database


When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100