Is there a better way? Datareader help needed to get this small app running.
Posted on 2005-04-19
If someone can provide me with a working solution I would be very pleased. I'm converting a website from classic asp to asp.net. I'm having some issues around looping through the recordset like I used to do with plain ASP. I dont think that binding to a datalist/datagrid will accomplish what I need to do. But then again I am a newbie to .NET. I basicly need to loop through a set of records and build up web links from the data in the recordset. Below you will find basicly what I'm trying to do. There proably are some syntax errors for instance in the <title> I used an old classic asp way of displaying variable data wher maybe I should have used an asp:label? I didnt know if I should have used any of the Page_Load event handlers. Thank you for your time. A perfect answer will get you a decent amount of points for you time.
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
Dim CurrentPage as integer
Dim JokeTypeID as integer
Dim PageTitle as String
'Grab the TypeID from the URL.
If Request.QueryString("Type") = "" Then
JokeTypeID = 1
JokeTypeID = Request.QueryString("Type")
'Set a page title depending on JokeTypeID .
If JokeTypeID = 1 Then
PageTitle = "Page Title 1"
ElseIf JokeTypeID = 2 Then
PageTitle = "Page Title 2"
ElseIf JokeTypeID = 3 Then
PageTitle = "Page Title 3"
ElseIf JokeTypeID = 4 Then
PageTitle = "Page Title 4"
'Grab the Page number from the URL for implementing paging later on.
If Request.QueryString("Page") = "" Then
CurrentPage = 1
CurrentPage = Request.QueryString("Page")
Dim SQLQuery as String
Dim Connection As SqlConnection
Dim mySQLCommand As SqlCommand
Dim mySQLDataReader As SqlDataReader
'Stored Procedure - Returns results like...
'125,1,Why Did the Chicken Cross the Road?
SQLQuery = "GetJokeList "& JokeTypeID
'Create new SQL Connection.
Connection = New SQLConnection("server=local;User id=sa;password=pass;database=Jokes")
'Create a command object
mySQLCommand = New SqlCommand(SQLQuery, Connection)
'Get the recordset
mySQLDataReader = mySQLCommand.ExecuteReader()
'Loop through the recordset and create urls.
'Write out the record.
Response.write("<li><a href='Joke.asp?Type="& mySQLDataReader.Item("JokeTypeID") &"&ID="& mySQLDataReader.Item("JokeID") &"'>"& mySQLDataReader.Item("JokeDescription") &"</a></li>")
<table border="0" width="500" cellpadding="0" cellspacing="0">
<td><% DisplayJokeLinks() %></td>