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.
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="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
DataSet dataset = null;
int position = 0;
void Page_Load(Object sender, EventArgs e) {
dataset = new DataSet();
if (!this.IsPostBack) {
position = 0;
ViewState["position"] = position;
}
else {
position = Convert.ToInt32(ViewState["position"]);
}
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\inetpub\\website.mdb;");
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT date, text FROM news ORDER BY id", connection);
adapter.Fill(dataset, "news");
updateNews();
}
void prev(object sender, System.EventArgs e) {
ViewState["position"] = ++position;
if (!updateNews()) {
ViewState["position"] = --position;
}
}
void next(object sender, System.EventArgs e) {
ViewState["position"] = --position;
if (!updateNews()) {
ViewState["position"] = ++position;
}
}
private bool updateNews() {
try {
lbl_date.Text = dataset.Tables["news"].Rows[position]["date"].ToString();
lbl_text.Text = dataset.Tables["news"].Rows[position]["text"].ToString();
}
catch (Exception e) {
return false;
}
return true;
}
</script>
<h1>News</h1>
<form runat="server">
<asp:button id="b_prev" onClick="prev" text="Prev" runat="server" />
<asp:button id="b_next" onClick="next" text="Next" runat="server" />
</form>
<h2><asp:label id="lbl_date" runat="server" /></h2>
<asp:label id="lbl_text" runat="server" />
<p><asp:label id="lbl_error" runat="server" /></p>