Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing Dataset in ViewState/Session

Status
Not open for further replies.

davewelsh

Programmer
Jun 26, 2001
71
CA
I wrote this a few days ago, but I forgot to put in a subject heading and no one replied.

Now I'm asking a slightly different question: is it feasible to store a DataSet in a ViewState or Session Variable? The reason I ask is that (I figure) some of a DataSet's benifits are lost if you have to re-Fill() the DataSet each time a page loads. I would like to fill it once and then just use it over and over, without having to open a database connection each time.

What is the popular opinion on this? Would it be a good thing to do, or is the overhead too great to be practical?

To give you an idea of what I'm talking about, here is a page on my site that has to Fill() the DataSet each Page_Load(). It would be easy to move the intialization of the DataSet into the "if (!this.IsPostBack)" section and then to get the object back from the ViewState otherwise.

Code:
<%@ Import Namespace=&quot;System.Data&quot; %>
<%@ Import Namespace=&quot;System.Data.OleDb&quot; %>

<script runat=&quot;server&quot;>

DataSet dataset = null;
int position = 0;

void Page_Load(Object sender, EventArgs e) {

    dataset = new DataSet();
    if (!this.IsPostBack) {
        position = 0;
        ViewState[&quot;position&quot;] = position;
    }
    else {
        position = Convert.ToInt32(ViewState[&quot;position&quot;]);
    }
    
    OleDbConnection connection = new OleDbConnection(&quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\inetpub\\website.mdb;&quot;);
    OleDbDataAdapter adapter = new OleDbDataAdapter(&quot;SELECT date, text FROM news ORDER BY id&quot;, connection);
    adapter.Fill(dataset, &quot;news&quot;);

    updateNews();
}

void prev(object sender, System.EventArgs e) {
    ViewState[&quot;position&quot;] = ++position;
    if (!updateNews()) {
        ViewState[&quot;position&quot;] = --position;
    }
}

void next(object sender, System.EventArgs e) {
    ViewState[&quot;position&quot;] = --position;
    if (!updateNews()) {
        ViewState[&quot;position&quot;] = ++position;
    }
}

private bool updateNews() {
    try {
        lbl_date.Text = dataset.Tables[&quot;news&quot;].Rows[position][&quot;date&quot;].ToString();
        lbl_text.Text = dataset.Tables[&quot;news&quot;].Rows[position][&quot;text&quot;].ToString();
    }
    catch (Exception e) {
        return false;
    }
    return true;
}
</script>

<h1>News</h1>

<form runat=&quot;server&quot;>
<asp:button id=&quot;b_prev&quot; onClick=&quot;prev&quot; text=&quot;Prev&quot; runat=&quot;server&quot; />
<asp:button id=&quot;b_next&quot; onClick=&quot;next&quot; text=&quot;Next&quot; runat=&quot;server&quot; />
</form>

<h2><asp:label id=&quot;lbl_date&quot; runat=&quot;server&quot; /></h2>

<asp:label id=&quot;lbl_text&quot; runat=&quot;server&quot; />

<p><asp:label id=&quot;lbl_error&quot; runat=&quot;server&quot; /></p>
 
thread855-310187 That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
That sounds good. I'll leave it as it is then and get the information from the database each time. It seems a shame though.

Perhaps you can verify or clarify this for me: if I use a session variable, the data is stored in ram and each page has access to this ram. If I use a ViewState, (of course it's stored in ram for processing, but) the data is cleared from ram each time a page unloads and it's brought back into ram in a new page via a hidden form field with a hash value.
 
umm What is your question? That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
It's not about how the Runtime uses the RAM but how your variable are accessible, to whom and for how long.

a Session Variable is accessible from all your web application, from all your classes and it's up to the Runtime to Drop these variables, and you really can't tell when that is going to happen.

With a dataset in a session variable you will most probably forcing the webserver to hold all that information for too long. When you have 5 users doing it than you might not care, but when you have 500, your server is going to go night-night...

When you Query the DataBase through ADO.NET the transactions are really quick and the Webserver CPU and Memory usage are optimized. There are many ways to pass information from one page to another.

Good luck!
Daren J. Lahey
Just another computer guy...
FAQ183-874 contains &quot;Suggestions for Getting Quick and Appropriate Answers&quot; to your questions.
Support your forums TODAY!
 
Zarcom: My question was &quot;is what I wrote correct?&quot;. (re: differences between session variables and using the viewstate)

Oh, and thanks for the replies, both of you.
 
oh ok. Your almost correct, the ViewState works slightly different than you described.
If I use a ViewState, (of course it's stored in ram for processing, but) the data is cleared from ram each time a page unloads and it's brought back into ram in a new page via a hidden form field with a hash value.


the data is cleared from RAM when the page is sent to the client. When the client performs a post back that view state is read back into memory from the hidden and hashed form field. When the client goes to a new page either through a server side redirect or a client side one, The viewstate is started fresh. There is no data about the previous page in it. That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top