Resources » Code Snippets » ASP.NET GridView

Grid view save edit and update


Posted Date:     Category: ASP.NET GridView    
Author: Member Level: Gold    Points: 7


this is a simple code for inserting,updating,and deleting the data



 


this is a simple code for editing,updating and deleting in gridview

Establish the Connection



This is the procedure for establishing the sql connection


string connection = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlDataAdapter DA;
DataSet ds = new DataSet();


ToBind Data



this is the format of binding the data to the grid
Here a Connection 'con' is instantiated and the connecction 'con' is opened
and closed inbetween the open and close lies the code for binding grid.


public void Bindgrid()
{
SqlConnection con = new SqlConnection(connection);
con.Open();
DA = new SqlDataAdapter("Select * from Category", con);
DA.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}


in your gridView property set show footer to 'TRUE"

To Save


to Save data the code is as follows

protected void BtnSave(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("insert into Category values('" + TextBox1.Text + "','" + TextBox2.Text + "')", con);
//Bindgrid();
cmd.ExecuteNonQuery();

con.Close();
Bindgrid();
}


TO Update



protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

TextBox textcategory = (TextBox)row.Cells[2].FindControl("TextBox4");

TextBox textDescription = (TextBox)row.Cells[3].FindControl("TextBox6");

SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("update Category set Description='" + textDescription.Text + "' where categoryName='" + textcategory.Text + "'", con);
cmd.ExecuteNonQuery();

con.Close();
GridView1.EditIndex = -1;
Bindgrid();

}

Canceling Edit


this is nothing but to cancel the edit


protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Bindgrid();
}


the procedure for editing is as follows


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



TO Delete



here is the snippet for deleting the data in the gridview


protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
//TextBox textcategory = (TextBox)row.FindControl("TextBox4");
Label lbldeleteID = (Label)row.FindControl("Label1");
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("delete from Category where categoryName='" + lbldeleteID.Text + "'", con);
cmd.ExecuteNonQuery();
Bindgrid();
con.Close();

}


To insert Element using RowCommand



this is an alternate method to insert a data using through the gridview row
and it is as follows


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
TextBox txtcategoryName = (TextBox)GridView1.FooterRow.FindControl("TextBox8");
TextBox txtDescription = (TextBox)GridView1.FooterRow.FindControl("TextBox7");
//Label lblDescription = (Label)GridView1.FooterRow.FindControl("Label2");
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("insert into Category values ('"+txtcategoryName.Text+"','"+txtDescription.Text+"')",con );
cmd.ExecuteNonQuery();
con.Close();
Bindgrid();
}


The sample Code is as follows

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.Configuration;
using System.Data;


public partial class Default2 : System.Web.UI.Page
{
string connection = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlDataAdapter DA;
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

Bindgrid();
}
}
protected void BtnSave(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("insert into Category values('" + TextBox1.Text + "','" + TextBox2.Text + "')", con);
//Bindgrid();
cmd.ExecuteNonQuery();

con.Close();
Bindgrid();
}
public void Bindgrid()
{
SqlConnection con = new SqlConnection(connection);
con.Open();
DA = new SqlDataAdapter("Select * from Category", con);
DA.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

}
//protected void Button1_Click(object sender, EventArgs e)
//{
// SqlConnection con = new SqlConnection();
// con.Open();
// SqlCommand cmd = new SqlCommand("delete * from Category where Category='"+TextBox1.Text+"',Description='"+TextBox2.Text+"')",con);
// cmd.ExecuteNonQuery();
// con.Close();

//}
//protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
//{
// GridView1.EditIndex = e.NewEditIndex;
// Bindgrid();
//}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

TextBox textcategory = (TextBox)row.Cells[2].FindControl("TextBox4");

TextBox textDescription = (TextBox)row.Cells[3].FindControl("TextBox6");

SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("update Category set Description='" + textDescription.Text + "' where categoryName='" + textcategory.Text + "'", con);
cmd.ExecuteNonQuery();

con.Close();
GridView1.EditIndex = -1;
Bindgrid();

}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
Bindgrid();
}
protected void TextBox6_TextChanged(object sender, EventArgs e)
{

}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
Bindgrid();
}
protected void Button7_Click(object sender, EventArgs e)
{

}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
//TextBox textcategory = (TextBox)row.FindControl("TextBox4");
Label lbldeleteID = (Label)row.FindControl("Label1");
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("delete from Category where categoryName='" + lbldeleteID.Text + "'", con);
cmd.ExecuteNonQuery();
Bindgrid();
con.Close();

}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
TextBox txtcategoryName = (TextBox)GridView1.FooterRow.FindControl("TextBox8");
TextBox txtDescription = (TextBox)GridView1.FooterRow.FindControl("TextBox7");
//Label lblDescription = (Label)GridView1.FooterRow.FindControl("Label2");
SqlConnection con = new SqlConnection(connection);
con.Open();
SqlCommand cmd = new SqlCommand("insert into Category values ('"+txtcategoryName.Text+"','"+txtDescription.Text+"')",con );
cmd.ExecuteNonQuery();
con.Close();
Bindgrid();
}
}


do check the attachment for gridview reference

Attachments
  • insert delete update (40129-1166-grid.txt)





  • Did you like this resource? Share it with your friends and show your love!


    Responses to "Grid view save edit and update"
    Author: Kapil Verma    25 Feb 2011Member Level: Silver   Points : 0
    nice code friend


    Author: kimosahan    26 Apr 2011Member Level: Silver   Points : 1
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;

    public partial class _Default : System.Web.UI.Page
    {
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter adp;
    DataSet ds;
    SqlDataReader dr;
    protected void Page_Load(object sender, EventArgs e)
    {
    Panel1.Visible = false;
    Panel2.Visible = false;
    Panel3.Visible = false;
    if (!IsPostBack)
    {
    bind();
    }
    }
    void bind()
    {

    con = new SqlConnection("server=localhost;uid=sa;pwd=student;database=master;");
    con.Open();
    adp = new SqlDataAdapter("exec deptselct ",con);

    ds = new DataSet();
    adp.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();

    con.Close();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
    Panel2.Visible = true;

    }
    protected void Button4_Click(object sender, EventArgs e)
    {
    con = new SqlConnection("server=localhost;uid=sa;pwd=student;database=master;");

    con.Open();
    cmd = new SqlCommand();
    cmd.CommandText = "deptins";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Connection = con;
    cmd.Parameters.Add("@name", SqlDbType.VarChar, 20).Value = TextBox1.Text;
    cmd.Parameters.Add("@id", SqlDbType.Int).Value = TextBox2.Text;
    dr = cmd.ExecuteReader();
    bind();
    con.Close();
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
    con = new SqlConnection("server=localhost;uid=sa;pwd=student;database=master;");

    con.Open();
    cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "deptdl";
    cmd.Connection = con;


    cmd.Parameters.Add("@id", SqlDbType.Int).Value = TextBox3.Text;
    dr = cmd.ExecuteReader();
    bind();
    con.Close();


    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
    GridView1.PageIndex = e.NewPageIndex;
    bind();
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
    Panel1.Visible = true;

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
    Panel3.Visible = true;
    }
    protected void Button6_Click(object sender, EventArgs e)
    {
    con = new SqlConnection("server=localhost;database=master;uid=sa;pwd=student;");
    con.Open();
    cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "deptup";
    cmd.Parameters.Add("@name",SqlDbType.VarChar,20).Value=TextBox4.Text;
    cmd.Parameters.Add("@id", SqlDbType.Int).Value = TextBox5.Text;
    dr = cmd.ExecuteReader();
    bind();
    con.Close();
    }
    }




    Author: Gaurav Balyan    24 Oct 2011Member Level: Bronze   Points : 0
    very nice tutorial really helped me


    Guest Author: devarajan     31 Dec 2011
    pls send some ifelse programmes with output in asp.net


    Author: Parthiban    30 Jul 2012Member Level: Bronze   Points : 0
    really helpful ..thanq..


    Guest Author: suresh     01 Nov 2012
    it's simply ow-some.....thanks a lot...


    Feedbacks      

    Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Change color and set tooltip of GridView row
    Previous Resource: Dynamic GridView
    Return to Resources
    Post New Resource
    Category: ASP.NET GridView


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    To perform insert update and delete  .  
    Active Members
    Today
      Last 7 Daysmore...

      Awards & Gifts
      Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
      2005 - 2012 All Rights Reserved.
      .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
      Articles, tutorials and all other content offered here is for educational purpose only.
      We are not associated with Microsoft or its partners.