Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii i m trying to delete or update data in grid view on click bt this code(below) gives an error plzz tell me what is wrong in this code..plzz.Thnxzz..


Error showing in this code is...when click delete..""The GridView 'GridView1' fired event RowDeleting which wasn't handled.""

Error showing in this code is...when click edit..""The GridView 'GridView1' fired event RowEditing which wasn't handled."""




code
C#
protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection("Data Source=DEEPAK-PC\\SQLEXPRESS;Initial Catalog=NewCrackers;uid=sa;pwd=sasa;");
        if (!IsPostBack)
        {
            bind();
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        Label lbl = (Label)row.FindControl("lblid");
        TextBox textname = (TextBox)row.FindControl("textbox1");
        TextBox textmarks = (TextBox)row.FindControl("textbox2");

        GridView1.EditIndex = -1;
        conn.Open();
        SqlCommand cmd = new SqlCommand("update  emp set marks=" + textmarks.Text + " , name='" + textname.Text + "' where rowid=" + lbl.Text + "", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        bind();
    }
    public void bind()
    {
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from emp", conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "emp");
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
        conn.Close();
    }
    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  emp where rowid=" + lbldeleteID.Text + "", conn);
 cmd.ExecuteNonQuery();
conn.Close();
bind();
}
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bind();
    }
Posted
Updated 6-Jan-14 21:18pm
v3
Comments
Karthik_Mahalingam 7-Jan-14 3:20am    
post your markup code..
amit1992 7-Jan-14 3:22am    
markuup??


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:gridview autogeneratecolumns="False" runat="server" id="GridView1"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True">
<columns>
<asp:templatefield headertext="IDNO">
<itemtemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("rowid")%>' />


<asp:templatefield headertext="Name">
<itemtemplate> <%#Eval("name")%>

<asp:templatefield headertext="Marks">
<itemtemplate><%#Eval("marks") %>



</form>

</body>
</html>
Karthik_Mahalingam 7-Jan-14 3:29am    
check my solution..
amit1992 7-Jan-14 3:40am    
@karthik sir its working for delete bt when i click on edit then update option is giving error as---""Object reference not set to an instance of an object.""

Make sure that your ASPX Gridview has this information..

ASP.NET
<asp:GridView ID="GridView1" runat="server"
        onrowdeleting="GridView1_RowDeleting"
        onrowediting="GridView1_RowEditing"
        onpageindexchanging="GridView1_PageIndexChanging"
        onrowcancelingedit="GridView1_RowCancelingEdit"
        onrowupdating="GridView1_RowUpdating" 
        >


Possible fix :

  • it might be with some other name like GridView1_RowDeleting_1 etc. Pls check the event names
  • if it is aleady there ,delete the same and create a new event for the above two and map it to the code behind event...
 
Share this answer
 
v2
Comments
amit1992 7-Jan-14 3:39am    
@karthik sir its working for delete bt when i click on edit then update option is giving error as---""Object reference not set to an instance of an object.""
Karthik_Mahalingam 7-Jan-14 4:00am    
amit,
as abishi suggested
check this site http://www.c-sharpcorner.com/UploadFile/9f0ae2/gridview-edit-delete-and-update-in-Asp-Net/
you wil get what u need.
amit1992 7-Jan-14 6:38am    
@karthik Sir wen i use row edit part then error is coming till nw..
plzz u tell me right code for remove that error...

Error is-
""Object reference not set to an instance of an object.""
Karthik_Mahalingam 7-Jan-14 7:31am    
did u tried with the above link ?
amit1992 7-Jan-14 23:00pm    
yes i hv tried with this.
HI
Please refer this link may it will help you


http://www.c-sharpcorner.com/UploadFile/9f0ae2/gridview-edit-delete-and-update-in-Asp-Net/
 
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