Do I need to retain the database after assigning it as data source to a gridview control?

Hi, I'm using vs2013, C# and web form
I have a method that is invoked when users clicks on a button on my web page.  The method retrieves data, puts in a dataset which I then assign that dataset to be the data source for a gridview control.  This gridview control will live through the life of this web page.  The application has been running fine without complaints about the dataset but I just noticed in my code in the finally block this dataset is Disposed();  

1.  Should I not dispose the dataset?  Why has it been working?
2.  Should I create this dataset as a global variable at the page class level so it won't get disposed when the method is finished, it went out of scope so will be dispose too.

Thank you.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
Are you populating the dataset every page load?

posting code may help.
lapuccaAuthor Commented:
This application only has one page.  the data set is populated when a button is clicked by the user on this page.
Kyle AbrahamsSenior .Net DeveloperCommented:
So once you set the grid's source and databind it . . . are there any post backs thereafter or is this a display only page?
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

lapuccaAuthor Commented:
This is a display only page with a grid and a couple of buttons.
Kyle AbrahamsSenior .Net DeveloperCommented:
post your code please, easiest way to validate and explain.  You can remove connection strings or anything identifying.
lapuccaAuthor Commented:
Attached is the method's code, thank you.
Kyle AbrahamsSenior .Net DeveloperCommented:
So since you're populating the dataset every time you can dispose of it.  Essentially once the databind is called the html is written using those values.   No need to hold onto it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lapuccaAuthor Commented:
It's good to know that databind writes out the value in html so I don't need to keep the dataset.  Thank you.
lapuccaAuthor Commented:
But, I'm still not sure the if the dataset if bind to the datagrid as the datasource then why can it be disposed?  I understand the html already got the value but shouldn't I keep the dataset because it is a data source for the grid?
Kyle AbrahamsSenior .Net DeveloperCommented:
So HTTP is stateless.  EG:  That page doesn't know if the grid has ever been bound before or if it's been bound 1000 times.

Here's an example:

User A hits that page first time.
Server sees a request for the page . . . runs it's code.
Databind is called, Html rendered, response sent back to user A.
User A hits the button
Server sees a request for the page, sees that the button hit the request . . . it has no knowledge about the DataSet from the backend code from the previous request.  (It does know the viewstate of the grid though).  

Because your code is re-fetching the data every time it's a fresh copy which is why it works.  (Also if any values changed the user would see the updated values).  Had you just said use the dataset without it being disposed or without storing it in the session or viewstate the call to the dataset would fail.

(EG:  Even if you made it a global variable and didn't dispose it.  The next post back would have that page have the dataset as null or blank).

I hope that helps.
lapuccaAuthor Commented:
Hi Kyle,
Thank you for the explanation.  I think I understand what your describing is that databind/set data source for the gridview, these data are stored in the ViewState memory for the page.  Even though the dataset is only created each time the users click the Retrieve button to retrieve a different record to display in the gridview contorl.  The page is always constructed with the data from the ViewState so no need to store it.  That makes sense if my interpretation is correct.  Much appreciate you sharing this important information.
Kyle AbrahamsSenior .Net DeveloperCommented:
  ds = iStar.performSearchReturnDataSet(sessonId, "xxxxxxxxxxxxxxxxxxx", parm);

Open in new window

You're passing in the sessionId to that function . . . that function can be going to the database, viewstate or session (viewstate is saved on the page, increases page size when pushing to client as it must be downloaded and pushed back to the server with each request.  Session is stored in RAM on the server), database is loading from sql.

Just a quick note that the whole dataset is loaded every time, not just one row.  But essentially everything that you're saying is correct.

A review of the pageState may help in the understanding:

lapuccaAuthor Commented:
The 2nd link explains the viewstate nicely too.  I believe I got.  The page is reconstructed using the ViewState value each time and that's why I don't need store the dataset value.  A new dataset is only generated when users clicks on the button and that new dataset value is then saved in the vewstate....   Thank you very much.
Kyle AbrahamsSenior .Net DeveloperCommented:
Bingo!  Happy coding.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.