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!

Here's the code from my news page.

Status
Not open for further replies.

davewelsh

Programmer
Jun 26, 2001
71
CA
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
Code:
int position
gets reset each time a button is pressed.

Does anyone know how to fix this:

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

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

DataSet dataset = null;
int position;

void Page_Load(Object sender, EventArgs e) {

	dataset = new DataSet();
	position = 0;

	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) {
	position++;
	updateNews();
}

void next(object sender, System.EventArgs e) {
	position--;
	updateNews();
}

private void 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) {
	}
}
</script>

<h1>News</h1>

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

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

<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>

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.
 
Here's updated code that seems to work properly. I just read something about ViewsState and this is what I came up with:

Tell me what you think (perhaps there's a better way?)
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>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top