Gridview displays old data

  • Thread starter Thread starter maggie
  • Start date Start date
M

maggie

hi,
I have a gridview that is based on parameters. When the select statement
does not return any rows, I want to display a label that says 'nothing
found'. It works the first time there is not data, but after that, it
displays what the previous select statement retrieved. How do I fix it so
that it doesn't do that? I am using a datareader. This is my code:
Code:
cn.Open();
System.Data.SqlClient.SqlDataReader dr  ;
dr = cmd.ExecuteReader();

if (dr.HasRows)
{
GridView1.DataSource = dr;
GridView1.DataBind();
}
lblNoData.Visible = (GridView1.Rows.Count == 0);


dr.Close();
cn.Close();


}
 
maggie said:
I have a gridview that is based on parameters. When the select statement
does not return any rows, I want to display a label that says 'nothing
found'. It works the first time there is not data, but after that, it
displays what the previous select statement retrieved. How do I fix it so
that it doesn't do that? I am using a datareader.

Your gridview is retaining the data from the previous time data was bound to
it. To clear this, set the gridview's datasource to null and rebind.

if (dr.HasRows)
{
GridView1.DataSource = dr;
GridView1.DataBind();
} else {
GridView1.DataSource = null;
GridView1.DataBind();
}
 
Thanks Ben. I will try it.

Ben Amada said:
Your gridview is retaining the data from the previous time data was bound to
it. To clear this, set the gridview's datasource to null and rebind.

if (dr.HasRows)
{
GridView1.DataSource = dr;
GridView1.DataBind();
} else {
GridView1.DataSource = null;
GridView1.DataBind();
}
 
Back
Top