Here's the code from my news page. I wanted to make it database driven. I don't have any experience with this, but it looks, to me, like it should work.
The page loads fine and the first entry comes up. If I hit next, the page just reloads; The exception (EOF) is caught and ignored. If I hit prev, the previous entry comes up, just as expected. If I hit next at that point, it goes back to the first one, but if I hit previous again, nothing happens. There are at least four records.
I think maybe my variable
gets reset each time a button is pressed.
Does anyone know how to fix this:
If you want to see this code in action, I have it running at Since I'm always changing stuff, it might not be working when you visit. Also the page won't work with less than Netscape 6 or Explorer 5.5 for now.
The page loads fine and the first entry comes up. If I hit next, the page just reloads; The exception (EOF) is caught and ignored. If I hit prev, the previous entry comes up, just as expected. If I hit next at that point, it goes back to the first one, but if I hit previous again, nothing happens. There are at least four records.
I think maybe my variable
Code:
int position
Does anyone know how to fix this:
Code:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
DataSet dataset = null;
int position;
void Page_Load(Object sender, EventArgs e) {
dataset = new DataSet();
position = 0;
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) {
position++;
updateNews();
}
void next(object sender, System.EventArgs e) {
position--;
updateNews();
}
private void 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) {
}
}
</script>
<h1>News</h1>
<h2><asp:label id="lbl_date" runat="server" /></h2>
<asp:label id="lbl_text" runat="server" />
<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>
If you want to see this code in action, I have it running at Since I'm always changing stuff, it might not be working when you visit. Also the page won't work with less than Netscape 6 or Explorer 5.5 for now.