Active Server Pages Programming (ASP)/Asp Programming
Expert: Srini Nagarajan - 9/25/2007
Questionhow to disply information from database use asp
AnswerHi
Here is simple code to retrieve and display from database
html><head>
<TITLE>dbsimple.asp</TITLE>
</head>
<body bgcolor="#FFFFFF">
<%
' this code opens the database
myDSN="DSN=Student;uid=student;pwd=magic"
set conntemp=server.createobject("adodb.connection")
conntemp.open myDSN
' this code retrieves the data
mySQL="select * from publishers where state='NY'"
set rstemp=conntemp.execute(mySQL)
' this code detects if data is empty
If rstemp.eof then
response.write "No records matched<br>"
response.write mySQL & "<br>So cannot make table..."
connection.close
set connection=nothing
response.end
end if
%>
<table border=1>
<%
' This code puts fieldnames into column headings
response.write "<tr>"
for each whatever in rstemp.fields
response.write "<td><B>" & whatever.name & "</B></TD>"
next
response.write "</tr>"
' Now lets grab all the records
DO UNTIL rstemp.eof
' put fields into variables
pub_id=rstemp("pub_id")
pub_name=rstemp("pub_name")
city=rstemp("city")
state=rstemp("state")
country=rstemp("country")
' write the fields to browser
cellstart="<td align=""top"">"
response.write "<tr>"
response.write cellstart & pub_id & "</td>"
response.write cellstart & pub_name & "</td>"
response.write cellstart & city & "</td>"
response.write cellstart & state & "</td>"
response.write cellstart & country & "</td>"
response.write "</tr>"
rstemp.movenext
LOOP
%>
</table>
<%
' Now close and dispose of resources
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
%>
</body></html>
-srini