Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
5.00/5 (1 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.


C#
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
Updated 20-Jun-12 23:28pm
v3
Comments
Vani Kulkarni 21-Jun-12 5:26am    
What is the error you are getting?
Code 89 21-Jun-12 5:39am    
wat u gettin??
[no name] 21-Jun-12 5:43am    
in update and delete query is not right.
Code 89 21-Jun-12 5:46am    
tel me d error u getttin
Nikil0012 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.

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..........
 
Share this answer
 
in command write update and delete query which will update the data. u have simply write select query.

C#
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
C#
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[^]
 
Share this answer
 
v2
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900