Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Experts,
plz suggest me the error in my code....
i.e., gridview edit,update,delete by clicking on edit button the table disapering .... can any one suggest that error..
this is my code..
my source code..

XML
<asp:GridView ID="gvdetails" runat="server" AutoGenerateColumns="false"
            AllowPaging="true" PageSize="5"
             OnRowEditing="gvdetails_RowEditing" OnRowDeleting="gvdetails_RowDeleting" OnRowUpdating="gvdetails_RowUpdating" DataKeyNames="UserId,UserName"
              OnRowCancelingEdit="gvdetails_RowCancelingEdit"  OnPageIndexChanging="gvdetails_PageIndexChanging">

        <PagerSettings Mode="NumericFirstLast" PageButtonCount="5" FirstPageText="Firstpage" NextPageText="Nextpage" />
        <Columns>
        <asp:TemplateField>
        <EditItemTemplate>
        <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" />
        <asp:ImageButton ID="imgbtnCancel" CommandName="Cancel" runat="server" ImageUrl="~/Images/Button cancel Icon.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
        </EditItemTemplate>
        <ItemTemplate>
        <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit Icon.jpg" ToolTip="Edit" Height="20px" Width="20px" />
        <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" runat="server" ImageUrl="~/Images/Delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
        </ItemTemplate>
        </asp:TemplateField>

       <asp:TemplateField HeaderText="UserName">
       <ItemTemplate>
       <asp:Label ID="lblitmusr" runat="server" Text='<%#Eval("UserName")%>'></asp:Label>
       </ItemTemplate>
       <EditItemTemplate>
       <asp:Label ID="lbleditusr" runat="server" Text='<%#Eval("UserName") %>'></asp:Label>
       </EditItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="City">
       <ItemTemplate>
       <asp:Label ID="lblitmcity" runat="server" Text='<%#Eval("City") %>' ></asp:Label>
       </ItemTemplate>
       <EditItemTemplate>
       <asp:Label ID="lbleditcity" runat="server" Text='<%#Eval("City") %>'></asp:Label>
       </EditItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="Designation">
       <ItemTemplate>
       <asp:Label ID="lblitmdesignation" runat="server" Text='<%#Eval("Designation")%>'></asp:Label>
       </ItemTemplate>
       <EditItemTemplate>
       <asp:Label ID="lbleditdesignation" runat="server" Text='<%#Eval("Designation") %>'></asp:Label>
       </EditItemTemplate>
       </asp:TemplateField>

        </Columns>

        </asp:GridView>
        <asp:Label ID="lblresult" runat="server"></asp:Label>




my aspx.cs code:

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }



    }
    protected void BindGridView()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from GridDetails", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            gvdetails.DataSource = ds;
            gvdetails.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gvdetails.DataSource = ds;
            gvdetails.DataBind();
            int columncount = gvdetails.Rows[0].Cells.Count;
            gvdetails.Rows[0].Cells.Clear();
            gvdetails.Rows[0].Cells.Add(new TableCell());
            gvdetails.Rows[0].Cells[0].ColumnSpan = columncount;
            gvdetails.Rows[0].Cells[0].Text = "No Records Found";

        }


    }

    protected void gvdetails_RowUpdating(object sender,GridViewUpdateEventArgs e)
    {
        int userid = Convert.ToInt32(gvdetails.DataKeys[e.RowIndex].Value.ToString());
        string username = gvdetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
        TextBox txtcity=(TextBox)gvdetails.Rows[e.RowIndex].FindControl("txtcity");
        TextBox txtDesignation=(TextBox)gvdetails.Rows[e.RowIndex].FindControl("txtDesignation");
        con.Open();
        SqlCommand cmd=new SqlCommand("update from GridDetails set City='"+txtcity.Text+"',Designation='"+txtDesignation.Text+"' where Userid="+userid,con);
        cmd.ExecuteNonQuery();
        con.Close();
        lblresult.Text=username + "Details Updated Successfully";
        gvdetails.EditIndex = -1;
        BindGridView();
        



    }
   
    protected void gvdetails_RowEditing(object sender,GridViewEditEventArgs e )
    {
        gvdetails.EditIndex = e.NewEditIndex;
        gvdetails.DataBind();
    }

    protected void gvdetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvdetails.EditIndex = -1;
        BindGridView();
    }
    protected void gvdetails_RowDeleting(object sender,GridViewDeleteEventArgs e)
    {


        int userid = Convert.ToInt32(gvdetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
        string username = gvdetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from GridDetails where UserId=" + userid, con);
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            BindGridView();
            lblresult.Text = username + "Details deleted successfully";

        }
    }
    protected void gvdetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvdetails.PageIndex = e.NewPageIndex;
        BindGridView();
Posted
Updated 7-Sep-12 1:26am
v2
Comments
__TR__ 7-Sep-12 6:56am    
Try replacing gvdetails.DataBind();
With
BindGridView();
in gvdetails_RowEditing()
Member 9376025 7-Sep-12 7:05am    
i changed there bindgridview();
then clicking on update button the error is occured "Object reference not set to an instance of an object."
__TR__ 7-Sep-12 7:10am    
I would suggest you to debug and check at which line the error occurs. I think it might be occurring in gvdetails_RowUpdating()
Member 9376025 7-Sep-12 7:12am    
ya in gvdetails_rowUpdating the error line is
SqlCommand cmd=new SqlCommand("update from GridDetails set City='"+txtcity.Text+"',Designation='"+txtDesignation.Text+"' where Userid="+userid,con);
__TR__ 7-Sep-12 7:18am    
Check to see what values are you getting for txtcity.Text, txtDesignation.Text and userid. Chances are one of them might be null.

Try this:
C#
protected void gvdetails_RowEditing(object sender,GridViewEditEventArgs e )
{
    gvdetails.EditIndex = e.NewEditIndex;
    //gvdetails.DataBind(); You missed your datasource here.
    BindGridView();
}



--Amit
 
Share this answer
 
Hello dude use this code , I have take some modification


in your aspx page .

XML
<asp:GridView ID="gvdetails" runat="server" AutoGenerateColumns="false"
            AllowPaging="true" PageSize="5"
             OnRowEditing="gvdetails_RowEditing" OnRowDeleting="gvdetails_RowDeleting" OnRowUpdating="gvdetails_RowUpdating" DataKeyNames="UserId,UserName"
              OnRowCancelingEdit="gvdetails_RowCancelingEdit"  OnPageIndexChanging="gvdetails_PageIndexChanging">

        <PagerSettings Mode="NumericFirstLast" PageButtonCount="5" FirstPageText="Firstpage" NextPageText="Nextpage" />
        <Columns>
        <asp:TemplateField>
        <EditItemTemplate>
        <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" />
        <asp:ImageButton ID="imgbtnCancel" CommandName="Cancel" runat="server" ImageUrl="~/Images/Button cancel Icon.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
        </EditItemTemplate>
        <ItemTemplate>
        <asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit Icon.jpg" ToolTip="Edit" Height="20px" Width="20px" />
        <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" runat="server" ImageUrl="~/Images/Delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
        </ItemTemplate>
        </asp:TemplateField>

       <asp:TemplateField HeaderText="UserName">
       <ItemTemplate>
       <asp:Label ID="lblitmusr" runat="server" Text='<%#Eval("UserName")%>'></asp:Label>
       </ItemTemplate>
       <EditItemTemplate>
<%--         <asp:Label ID="Label1" runat="server" Text='<%#Eval("UserName") %>'></asp:Label> // line Comment by sanwar--%>
       <asp:TextBox ID="lbleditusr" runat="server" Text='<%#Eval("UserName") %>'></asp:TextBox>  <%--// line add by sanwar--%>
       </EditItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="City">
       <ItemTemplate>
       <asp:Label ID="lblitmcity" runat="server" Text='<%#Eval("City") %>' ></asp:Label>
       </ItemTemplate>
       <EditItemTemplate>
<%--  <asp:Label ID="Label1" runat="server" Text='<%#Eval("City") %>'></asp:Label>      // line Comment by sanwar--%>
       <asp:TextBox ID="lbleditcity" runat="server" Text='<%#Eval("City") %>'></asp:TextBox>        <%--// line add by sanwar--%>

       </EditItemTemplate>
       </asp:TemplateField>
       <asp:TemplateField HeaderText="Designation">
       <ItemTemplate>
       <asp:Label ID="lblitmdesignation" runat="server" Text='<%#Eval("Designation")%>'></asp:Label>
       </ItemTemplate>
       <EditItemTemplate>
       <%--         <asp:Label ID="lbleditdesignation" runat="server" Text='<%#Eval("Designation") %>'></asp:Label> // line Comment by sanwar--%>
       <asp:TextBox ID="lbleditdesignation" runat="server" Text='<%#Eval("Designation") %>'></asp:TextBox>  <%--// line add by sanwar--%>
       </EditItemTemplate>
       </asp:TemplateField>

        </Columns>

        </asp:GridView>
        <asp:Label ID="lblresult" runat="server"></asp:Label>




And in your aspx.cs page


protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           BindGridView();
       }



   }
   protected void BindGridView()
   {
       con.Open();
       SqlCommand cmd = new SqlCommand("select * from GridDetails", con);
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       DataSet ds = new DataSet();
       da.Fill(ds);
       con.Close();
       if (ds.Tables[0].Rows.Count > 0)
       {
           gvdetails.DataSource = ds;
           gvdetails.DataBind();
       }
       else
       {
           ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
           gvdetails.DataSource = ds;
           gvdetails.DataBind();
           int columncount = gvdetails.Rows[0].Cells.Count;
           gvdetails.Rows[0].Cells.Clear();
           gvdetails.Rows[0].Cells.Add(new TableCell());
           gvdetails.Rows[0].Cells[0].ColumnSpan = columncount;
           gvdetails.Rows[0].Cells[0].Text = "No Records Found";

       }


   }

   protected void gvdetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {
       GridViewRow row = (GridViewRow)gvdetails.Rows[e.RowIndex]; // line add by sanwar
       int userid = Convert.ToInt32(gvdetails.DataKeys[e.RowIndex].Value.ToString());
       string username = gvdetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
       TextBox txtcity = (TextBox)row.FindControl("lbleditcity");// line add by sanwar
       // TextBox txtcity = (TextBox)gvdetails.Rows[e.RowIndex].FindControl("txtcity"); // line Comment by sanwar
       TextBox txtDesignation = (TextBox)row.FindControl("lbleditdesignation");// line add by sanwar
       //TextBox txtDesignation = (TextBox)gvdetails.Rows[e.RowIndex].FindControl("txtDesignation");// line Comment by sanwar
       con.Open();
       //SqlCommand cmd = new SqlCommand("update from GridDetails set City='" + txtcity.Text + "',Designation='" + txtDesignation.Text + "' where Userid=" + userid, con);
       SqlCommand cmd = new SqlCommand("update  GridDetails set City='" + txtcity.Text + "',Designation='" + txtDesignation.Text + "' where Userid=" + userid, con); // line Modify by sanwar

       cmd.ExecuteNonQuery();
       con.Close();
       lblresult.Text = username + "Details Updated Successfully";
       gvdetails.EditIndex = -1;
       BindGridView();




   }

   protected void gvdetails_RowEditing(object sender, GridViewEditEventArgs e)
   {
       gvdetails.EditIndex = e.NewEditIndex;
       // gvdetails.DataBind(); // line Comment by sanwar  Note: your table not display because your table data has been lost ,No Bind the data  this region
       BindGridView(); // line add by sanwar
   }

   protected void gvdetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
   {
       gvdetails.EditIndex = -1;
       BindGridView();
   }
   protected void gvdetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
   {


       int userid = Convert.ToInt32(gvdetails.DataKeys[e.RowIndex].Values["UserId"].ToString());
       string username = gvdetails.DataKeys[e.RowIndex].Values["UserName"].ToString();
       con.Open();
       SqlCommand cmd = new SqlCommand("delete from GridDetails where UserId=" + userid, con);
       int result = cmd.ExecuteNonQuery();
       con.Close();
       if (result == 1)
       {
           BindGridView();
           lblresult.Text = username + "Details deleted successfully";

       }
   }
   protected void gvdetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       gvdetails.PageIndex = e.NewPageIndex;
       BindGridView();
   }
 
Share this answer
 
v3
Comments
Member 9376025 8-Sep-12 1:12am    
i changed the code as above but the same error is coming....
"Object reference not set to an instance of an object."
Can u help me...
Member 9376025 8-Sep-12 6:31am    
I solved my problem..
in the above code
insted of this two lines
TextBox txtcity = (TextBox)row.FindControl("lbleditcity");
TextBox txtDesignation = (TextBox)row.FindControl("lbleditdesignation");

replace this lines
TextBox txtcity = (TextBox)row.FindControl("txteditcity");
TextBox txtDesignation = (TextBox)row.FindControl("txteditdesignation");

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