ASP.NET - how to insert ,edit, select ,updatein gridview using dataset

Asked By indramani tiwary
09-Feb-12 02:09 AM
gridview
  Somesh Yadav replied to indramani tiwary
09-Feb-12 02:17 AM
Hi you can just gothrough the below link it is explained very clearly with an example for insert,edit,update,select gridview using dataset.

http://wiki.asp.net/page.aspx/1360/add-edit-update-delete-gridview/

http://www.aspdotnetcodes.com/GridView_Insert_Edit_Update_Delete.aspx
  Lalit M. replied to indramani tiwary
09-Feb-12 02:32 AM
Hi..

get more sample code & article here.

http://dotnetgeeks.wordpress.com/2009/11/19/insert-select-edit-update-and-delete-in-gridview-with-datatable-as-datasource/

http://www.csharpaspnetarticles.com/2009/06/gridview-objectdatasource-insert-update.html

http://www.123aspx.com/redir.aspx?res=36275
  dipa ahuja replied to indramani tiwary
09-Feb-12 02:56 AM
► Add add commandField in your gridview:
 
<asp:CommandField ButtonType="Button"
ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
 
►Implement the following events:
 
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
 
.CS Code
 
static string ConnString ="ConnectionString";
protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    //Code to bind gridview
  }
 
}
 
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
  GridView1.EditIndex = e.NewEditIndex;
    //Code to bind gridview
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
  int id = int.Parse(((Label)GridView1.Rows[e.RowIndex].FindControl("lblid")).Text);//ID
  string name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname")).Text;
 
  SqlConnection connect = new SqlConnection(ConnString);
  connect.Open();
 
  string q = "Update people set name=@name where id=@id";
 
  SqlCommand comm = new SqlCommand(q, connect);
  comm.Parameters.AddWithValue("name", name);
  comm.Parameters.AddWithValue("id", id);
  comm.ExecuteNonQuery();
  connect.Close();
 
  GridView1.EditIndex = -1;
 
    //Code to bind gridview
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
  GridView1.EditIndex = -1;
  BindGrid();
 
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
  int id = int.Parse(((Label)GridView1.Rows[e.RowIndex].FindControl("lblid")).Text);//ID
 
  SqlConnection connect = new SqlConnection(ConnString);
  connect.Open();
 
  string q = "Delete from table where id=@id";
 
  SqlCommand comm = new SqlCommand(q, connect);
  comm.Parameters.AddWithValue("id", id);
  comm.ExecuteNonQuery();
  connect.Close();
 
  GridView1.EditIndex = -1;
 
    //Code to bind gridview
}
 
 
Create New Account
help
Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class vendorview : System.Web.UI.Page { DML_DDL cls = new DML_DDL(); private const int _firstEditCellIndex = 2; / / public event EventHandler SelectedIndexChanged; / / public event select * from vendorreg", "vendorreg"); gv.DataSource = cls.ds; gv.DataBind(); } protected void gv_RowEditing(object sender, GridViewEditEventArgs e) { gv.EditIndex = e.NewEditIndex; } public void gv_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e) { GridViewRow row = gv.Rows[e.RowIndex]; } / / protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e) / / { / / / / Retrieve the row being edited. / / / / int index = gv.EditIndex; / / / / GridViewRow row = gv.Rows[index Controls[0])).Text; / / / / Reset the edit index. / / gv.EditIndex = -1; / / } protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridView _gridView = (GridView)sender; if (e.RowIndex > -1) { / / Loop though the columns to find false }); _sampleData = dt; / / Repopulate the GridView this.gv.DataSource = _sampleData; this.gv.DataBind(); } } / / html part <%@ Page Language = "C#" AutoEventWireup = "true" CodeFile = "vendorview.aspx.cs" Inherits = "vendorview" %> <!DOCTYPE html PUBLIC "- / / W3C / / DTD transitional.dtd"> <html xmlns = "http: / / www.w3.org / 1999 / xhtml"> <head runat = "server"> <title> Untitled Page< / title> < / head> <body> <form id = "form1" runat = "server"> <div> <asp:GridView ID = "gv" runat = "server the events rowEditing, RowUpdating and rowCancelEdit to edit the gridview: protected void GridView1_RowEditing( object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; BindGrid(); } protected void GridView1_RowUpdating( object sender, GridViewUpdateEventArgs e) { int id
Fill(ds); GridView2.DataSource = ds; GridView2.DataBind(); } Code to delete protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e) { string strEmpId = GridView1.Rows[e.RowIndex].Cells[1].Text; / / FOR GETTING ENPID string deleteQuery Write("<script> alert('Record Deleted')< / script> "); getdata(); } Code to edit protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e) { GridView2.EditIndex = e.NewEditIndex; getdata(); } Code to Cancel protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView2.EditIndex = -1; getdata(); } Code to Update protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e) { string strEmpId = GridView1.Rows[e.RowIndex].Cells[1].Text; / / FOR GETTING ENPID string strEmpName getdata(); Response.Write("<script> alert('Record Updated')< / script> "); } Try this code and let me know. <% @ Page Language = "C#" AutoEventWireup = "true" CodeFile = "GridViewComplete.aspx.cs" Inherits = "GridViewComplete" %> < !DOCTYPE htm l PUBLIC "- / / W3C UI . WebControls . WebParts ; using System . Web . UI . HtmlControls ; public partial class GridViewComplete : System . Web . UI . Page { protected void Page_Load ( object sender , EventArgs e ) { if (! IsPostBack ) { BindGrid (); } } public void BindGrid () { if ( Session ASC" ; break ; case SortDirection . Descending : newSortDirection = "DESC" ; break ; } return newSortDirection ; } protected void GridView1_RowDeleting ( object sender , GridViewDeleteEventArgs e ) { int ID = ( int ) GridView1 . DataKeys [ e . RowIndex ]. Value ; / / Query the database and get the and delete it. lblMsg . Text = "Deleted Record Id" + ID . ToString (); } protected void GridView1_RowEditing ( object sender , GridViewEditEventArgs e ) { GridView1 . EditIndex = e . NewEditIndex ; BindGrid (); } protected void GridView1_RowUpdating ( object sender , GridViewUpdateEventArgs e ) { / / Retrieve the
Fill(ds); GridView2.DataSource = ds; GridView2.DataBind(); } Code to delete protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e) { string strEmpId = GridView1.Rows[e.RowIndex].Cells[1].Text; / / FOR GETTING ENPID string deleteQuery Write("<script> alert('Record Deleted')< / script> "); getdata(); } Code to edit protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e) { GridView2.EditIndex = e.NewEditIndex; getdata(); } protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView2.EditIndex = -1; getdata(); } Code to Update protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e) { string strEmpId = GridView1.Rows[e.RowIndex].Cells[1].Text; / / FOR GETTING ENPID string strEmpName program using System.Data.SqlClient; using System.IO; public partial class Gallery : System.Web.UI.Page { / / here i declare some variables that will be used below inside the code. String fn Open(); / / if the connection will be closed the below code poen the connection when the page will be loaded. if (cnn.State = = ConnectionState.Closed) { cnn.Open(); } cnn.Close(); if (IsPostBack = = false used to delete of the row record from the database protected void GridView1_RowDeleting( object sender, GridViewDeleteEventArgs e) { try { if (cnn.State = = ConnectionState.Closed) { cnn.Open(); } / / inside the id variable i am MapPath( "images" ) + " \ " + image); } catch { } cmd.ExecuteNonQuery(); cmd.Dispose(); grd_bind(); } catch { } } protected void GridView1_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; grd_bind(); } protected void GridView1_RowEditing( object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e
want to do edit, delete with gridview in vb.net. protected void GridView1_RowEditing( object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; FillGrid(); } protected void GridView1_RowCancelingEdit( object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; FillGrid(); } protected void GridView1_RowUpdating( object sender, GridViewUpdateEventArgs e) { ConnectionStringSettings mysetting = ConfigurationManager.ConnectionStrings[ "ConnStr" ]; SqlConnection conn = new SqlConnection(mysetting.ConnectionString); TextBox t1 = (TextBox MyData" ); da.Fill(ds); conn.Close(); GridView1.EditIndex = -1; FillGrid(); } protected void GridView1_RowDeleting( object sender, GridViewDeleteEventArgs e) { ConnectionStringSettings mysetting = ConfigurationManager.ConnectionStrings[ "ConnStr" ]; SqlConnection conn = new SqlConnection(mysetting.ConnectionString); string t1 = ( string UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class GridView_Pack : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { } protected void GridView1_PageIndexChanged(Object sender, EventArgs e) { / / when the user navigates to a different page. / / Calculate the current page number. int currentPage = GridView1.PageIndex + 1; / / Display the current page number. Info.Text = "You are currently seeing Page " + currentPage.ToString() + " of " + GridView1.PageCount.ToString() + "." ; } protected
to dropdownlist value and put the below code on droplown selectedIndex change event, not on page load event using (SqlConnection conn = new SqlConnection( "Your Connection string here" )) { conn.Open(); string sqlQuery Fill(ds); GridView2.DataSource = ds; GridView2.DataBind(); } Code to delete protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e) { string strEmpId = GridView1.Rows[e.RowIndex].Cells[1].Text; / / FOR GETTING ENPID string deleteQuery Write("<script> alert('Record Deleted')< / script> "); getdata(); } Code to edit protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e) { GridView2.EditIndex = e.NewEditIndex; getdata(); } Code to cancel protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView2.EditIndex = -1; getdata(); } Code to Update protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e) { string strEmpId = GridView1.Rows[e.RowIndex].Cells[1].Text; / / FOR GETTING ENPID string strEmpName it generates the following event Handlers for these commands: 1. protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) 2. protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 3. protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) In properties winidow click on GridView1_RowEditing, GridView1_RowCancelingEdit protected void Page_Load( object sender, EventArgs e