Asp.net gridview edit button not refreshing
When I enter new data and click the refresh button, they keep the old data (the data I want to refresh).
public void fill()
{
SqlCommand cmd = new SqlCommand("select * from school ",con );
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
GridView1.DataSource = rd;
GridView1.DataBind();
con.Close();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
fill();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
string stu =((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
int age = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
SqlCommand cmd = new SqlCommand("UPDATE school SET stu_name=@stu_name,age=@age where id=@id ", con);
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.Parameters.Add(new SqlParameter("@stu_name", stu));
cmd.Parameters.Add(new SqlParameter ("@age",age));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
fill();
}
the problem is that the values assigned to name, age are existing values in the database and not new values that I entered at runtime can anyone help me? thank you in advance
+3
source to share
2 answers
Here's some information on the lifecycle of a web form. I think all you need is to wrap fill();
in instructions if
. Since it page_load
happens before your event handler, you are reloading from the db on top of the values you entered.
if(!PostBack)
{
fill();
}
0
source to share