how to refresh griedview in asp.net after delete

Posted by Shanky11 under ASP.NET on 9/21/2013 | Points: 10 | Views : 809 | Status : [Member] | Replies : 5
protected void GridCart_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int index = Convert.ToInt32(e.CommandArgument);
int hdnimageid = Convert.ToInt32(((HiddenField)GridCart.Rows[index].Cells[0].FindControl("hdnimageid")).Value);
con.Open();
SqlCommand cmd = new SqlCommand("Delete from temporder where pid='" + hdnimageid + "'", con);
cmd.ExecuteNonQuery();
con.Close();

loadgridnew();


}
}
public void loadgridnew()
{
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("select * from temporder where ipaddress='" + ipAddress + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
GridCart.DataSource = dt;
GridCart.DataBind();
}what is the problem in it why page is not refreshing??????
<asp:ButtonField Text="Delete" CommandName="Delete" />




Responses

Posted by: Bandi on: 9/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
cmd.ExecuteNonQuery();con.Close();
loadgridnew();


in the above code you are closing connection before binding.......
loadgridnew() again using con object to return data from database.... so check this scenario by closing connection after loadgridnew().....

loadgridnew();
con.close();


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
refer similar problem here
http://www.daniweb.com/web-development/aspnet/threads/355695/grid-view-not-refreshing-

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/23/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You need to call the loadgridnew(); after delete.

Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nismeh on: 9/23/2013 [Member] Starter | Points: 25

Up
0
Down
Please remember that You can only close the connection if you are done with db entities. So make habit to close connection at end of block.

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 9/23/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

change the code like this

if (e.CommandName == "Delete")
{
int index = Convert.ToInt32(e.CommandArgument);
int hdnimageid = Convert.ToInt32(((HiddenField)GridCart.Rows[index].Cells[0].FindControl("hdnimageid")).Value);
con.Open();
SqlCommand cmd = new SqlCommand("Delete from temporder where pid='" + hdnimageid + "'", con);
cmd.ExecuteNonQuery();
con.Close();


con.Open();
loadgridnew();
con.Close();


}

Mark as Answer if its helpful to you

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response