Writing Your First ASP Application ( Quick Start ) - Showing The List Of Users
(Page 5 of 8 )
Now that the users names are being added to the database when they login to your website, now we need some method to show them on the screen. Below is a sample web page that will display the list of users. It merely runs through all of the entries in the UsersOnline table and displays them to the screen.
showusers.asp
<%@ LANGUAGE="VBSCRIPT" %>Users currently on the website:
<HR>
<%
dim conn
dim strsql
dim mycount
‘ Create a SQL statement to insert the name into our table
strsql = "select UserName from UsersOnline”
‘ Open the database using our DSN
strconn = “DSN=UserBase”
set conn = server.createobject("adodb.connection")
conn.open strconn
‘ Open the recordset containing user names
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open strsql, conn, 3,3
‘ Set the mycount variable to contain the number of users in the database
mycount=rs.recordcount
if mycount < 1 then
‘ There were no users logged in
response.write “None”
else
for a = 1 to mycount
‘ Write out the user names
response.write(rs(“UserName”))
if a < mycount then
rs.movenext
end if
next a
endif
‘ Close the recordset
rs.close
set rs = nothing
‘ Close the database connection
conn.close
set conn = nothing
%>
The above example mixes HTML and VBScript together to achieve the desired results. As long as you keep the Scripting code within the <% and %> delimiters you can utilize both interchangeably within the same ASP page.
Next: Simplifying The Code >>
More ASP Articles
More By Rich Smith