Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hello
 
I am trying to edit update and delete in grid view. My code is as follows.
Id is auto incremental.
But now update and delete is not working please help.
 

    public partial class MainTable : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
 
        protected void Page_Load(object sender, EventArgs e)
        {
            //SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
            if (!IsPostBack)
            {
                bind();
            }
        }
 
        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
 
        }
 
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row=(GridViewRow)GridView1.Rows[e.RowIndex];
            Label lbldeleteid = (Label)row.FindControl("lblID");
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_SqlImage", conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            bind();
 

 
        }
        public void bind()
        {
 
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tbl_SqlImage", conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "emp");
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
            conn.Close();
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();
            //GridView1.DataBind();
        }
 
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            Label lblID = (Label)row.FindControl("lblID");
            TextBox textName = (TextBox)row.FindControl("txtName");
            TextBox textDesc = (TextBox)row.FindControl("txtDesc");
            TextBox textImage = (TextBox)row.FindControl("txtImage");
            TextBox textActive = (TextBox)row.FindControl("txtActive");
            TextBox textCreatedBy = (TextBox)row.FindControl("txtCreatedBy");
            TextBox textCreatedDate = (TextBox)row.FindControl("txtCreatedDate");
            GridView1.EditIndex = -1;
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_SqlImage", conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            bind();
            //GridView1.DataBind();
            
        }
 
        
 
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            bind();
 
        }
 
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
             GridView1.EditIndex = -1;
             bind();
        }    
 

        
    }
 
Please help what's wrong with the code.
Please help.
Posted 21-Jun-12 0:17am
Edited 21-Jun-12 0:28am
v3
Comments
Vani Kulkarni at 21-Jun-12 5:26am
   
What is the error you are getting?
Code 89 at 21-Jun-12 5:39am
   
wat u gettin??
Code 89 at 21-Jun-12 5:46am
   
tel me d error u getttin
Nikil0012 at 21-Jun-12 6:15am
   
Hello sir/madam
 
My new query is this,
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblID = (Label)row.FindControl("lblID");

TextBox textName = (TextBox)row.FindControl("txtName");
TextBox textDesc = (TextBox)row.FindControl("txtDesc");
TextBox textImage = (TextBox)row.FindControl("txtImage");
TextBox textActive = (TextBox)row.FindControl("txtActive");
TextBox textCreatedBy = (TextBox)row.FindControl("txtCreatedBy");
TextBox textCreatedDate = (TextBox)row.FindControl("txtCreatedDate");
GridView1.EditIndex = -1;
conn.Open();

//SqlCommand cmd = new SqlCommand("update emp set marks=" + textmarks.Text + " , name='" + textname.Text + "' where rowid=" + lbl.Text + "", conn);
SqlCommand cmd = new SqlCommand("UPDATE tbl_SqlImage set Name=" + textName.Text + " , Description='" + textDesc.Text + " , Image='" + textImage.Text + " , Active='" + textActive.Text + " , CreatedBy='" + textCreatedBy.Text + " , CreatedDate='" + textCreatedDate.Text + "' where ID=" + lblID.Text + "", conn);


cmd.ExecuteNonQuery();
conn.Close();
bind();
//GridView1.DataBind();
 
Now i am getting an error object reference not set to an instance of the object.
 
i wnat to know also that createddate.text is write or i have to convert it.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

use this :
 
 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row=(GridViewRow)GridView1.Rows[e.RowIndex];
            Label lbldeleteid = (Label)row.FindControl("lblID");
            conn.Open();
            SqlCommand cmd = new SqlCommand("delete * FROM tbl_SqlImage where id_field='"+lbldeleteid+"'", conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            bind();
 
 
 
        }
 

 
 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            Label lblID = (Label)row.FindControl("lblID");
            TextBox textName = (TextBox)row.FindControl("txtName");
            TextBox textDesc = (TextBox)row.FindControl("txtDesc");
            TextBox textImage = (TextBox)row.FindControl("txtImage");
            TextBox textActive = (TextBox)row.FindControl("txtActive");
            TextBox textCreatedBy = (TextBox)row.FindControl("txtCreatedBy");
            TextBox textCreatedDate = (TextBox)row.FindControl("txtCreatedDate");
            GridView1.EditIndex = -1;
            conn.Open();
            SqlCommand cmd = new SqlCommand("update tablename set name='"+textname.Text+"',desc='"+txtdesc.Text+"' where id='"+lblID+"', conn); //  Here set Update Query to update the Field.
            cmd.ExecuteNonQuery();
            conn.Close();
            bind();
            //GridView1.DataBind();
            
        }
 

this will help you..........
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

in command write update and delete query which will update the data. u have simply write select query.
 
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row=(GridViewRow)GridView1.Rows[e.RowIndex];
            Label lbldeleteid = (Label)row.FindControl("lblID");
            conn.Open();
            SqlCommand cmd = new SqlCommand("delete FROM tbl_SqlImage where id="+ lbldeleteid.Text, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            bind();
 
        }
in update write
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
 

SqlCommand cmd = new SqlCommand("update  tbl_SqlImage set Name='"+ textName.text +"' where id="+ lbldeleteid.Text, conn);
}
 
for more look in this article[^]
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

hi
 
refer this link
 
Edit,delete in gridview in asp.net[^]
  Permalink  

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

  Print Answers RSS
0 BillWoodruff 330
1 Mathew Soji 309
2 Maciej Los 295
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 217
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,025


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 29 Jun 2012
Copyright © CodeProject, 1999-2014
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