Reset datagrid

L

Lee Price

Hi all,

I have a bound datagrid which is filled with data and
displayed.Now on the click of a button I need to refill
the datagrid with new data but the problem is the
datagrid mixes up the new data with the old data.

I have stored the data in a session variable which seems
to get updated although I am not altering the value of
the session variable

does binding a datagrid to a session variable update the
session variable also.

how do i reset my datagrid to take fresh values
 
S

SMG

Dear Lee....
Please find the code attached with below, this might solve ur problem.
Steps to check: Once you load the page with data, refresh it (F5), then
change the data at the back end and again refresh you will see the changes
are not reflected, then click the button on the page it will get the fresh
data from the server.

Regards,
Shailesh MG

Code ====================
/* you should have a datagrid1 and button1 on the aspx page and the proper
connection string.
===========================
private void Page_Load(object sender, System.EventArgs e)

{
ConnStr = "server=ILDC1\\SQLRND;initial catalog=Northwind;uid=sa;pwd=";
// Change this as per your need

if (Session["Data"] == null )
GetData();
else
{
ds = (DataSet)Session["Data"];
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}
}

private void GetData()
{
SqlConnection Conn = new SqlConnection(ConnStr);
CommStr = new StringBuilder("");
ds = new DataSet();
CommStr.Append(" select P.ProductID, P.ProductName, C.CategoryName,
UnitPrice from products P, Categories C where ");
CommStr.Append(" C.categoryid = P.categoryid order by productName ");
SqlDataAdapter sda = new SqlDataAdapter(CommStr.ToString() ,Conn );
sda.Fill(ds,"First");
Session["Data"] = ds;
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}

private void Button1_Click(object sender, System.EventArgs e)
{
GetData();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top