Link to home
Start Free TrialLog in
Avatar of cay187
cay187

asked on

load dataset once use dataview throughout page asp.net c#

i've been using asp for years now and dont have much experience with OOP.  i have a simple question that i cant find the answer to.
How can i load a dataset(2 datatables) on the Page_load() once and the use those tables on different events.

i have a ddl and a gridview.  on the page_load i want to load and populate the dataset - know how to do this
i have a button next to the ddl  user selects ddl value and hits button - know how to do this
onclick event of the button is onclick="button_click"
is there a way that i dont have t requery the DB and just use the datatable that i loaded

page_load()
{
   
 //connect and fill dataset
ds.Tables["Yards"]
//bind the ddl
ddl_yards.DataSource = ds.Tables["Yards"];


ds.Tables["Equip"] // datatable just made and not used yet - make this public so that the other functions can see it.  how???????

}

void button_click(Object Sender, EventArgs e)
{
  // i dont want to reconnect to the db to access the equip table every time the user click this button.
  // how can i access the "equip" datatable from inside of here that was made on the Page_load()?  so that i can bind it to a gridview - dont know how to do????????
 // do i make the dataset public? private?
 
 GridView1.DataSource = ds.Tables["Equip"];
 GridView1.DataBind();


}

Avatar of YZlat
YZlat
Flag of United States of America image

just declare your dataset as public outside the Page_load event
public static DataSet ds=null;
Avatar of devsolns
devsolns

keep in mind asp.net is stateless.  if you dont want to requery you need to use some sort of sessionstate.
ASKER CERTIFIED SOLUTION
Avatar of devsolns
devsolns

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cay187

ASKER

so if i load the dataset to the client the first time i need to cache it in order to reuse it again?  or save that dataset into session variables to reuse it on later events?
yes when you load it for the first time you can store it using session variable.  just be careful with hundreds/thousands of users this will eat server resources.