View Full Version : Problem looping db info.
Mhtml
08-25-2002, 04:10 AM
I'm learning ADO and I cut and pasted an example from w3schools and changed the db location and table names but it wouldn't loop. What is wrong?
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open Server.MapPath("countries.mdb")
set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Select * from countries", conn
for each x in rs.fields
response.write(x.name)
response.write(" = ")
response.write(x.value)
next
%>
It justs writes the first value and nothing else...I've included the DB for download
nanao56
08-25-2002, 06:34 AM
The code that you have is geared towards displaying the recordset column names... If you notice your output of that particular code is
ID = 1Country = Australia
where there are only 2 column names in the table; ID and Country.
With the FOR EACH, you are instructing ASP to go through each of the data points in a horizontal dimension.... (hopefully that makes sense).
Take a look at the code that I've got below. It's a dynamic piece of ASP code that will display results from any query. Take note of how the FOR loop is used to display the column names and the DO UNTIL End of Recordset code is used to go through each value in the recordset.
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open Server.MapPath("countries.mdb")
set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Select * from countries", conn
showblank=" "
shownull="-null-"
%>
<table>
<tr>
<%'Put Headings On The Table of Field Names
for each heading in rs.fields%>
<td><b><%=heading.name%></B></TD>
<% next %>
</tr>
<% ' Display all Table Columns
DO UNTIL rs.eof %>
<tr>
<% for each record in rs.fields
fieldvalue=record.value
if isnull(fieldvalue) then
fieldvalue=shownull
end if
if trim(fieldvalue)="" then
fieldvalue=showblank
end if%>
<td valign=top><%=fieldvalue%></td>
<% next %>
</tr>
<%rs.movenext
LOOP
rs.close
Set rs = Nothing
conn.close
Set Conn = Nothing
%>
</table>
The code above was adapted from http://www.learnasp.com/learn/dbsimple.asp. Check out the rest of the site and it have some good examples
Mhtml
08-25-2002, 06:46 AM
ah, that makes perfect sense. I've re-made it using most of which you've shown I've figured it out now, I even made a little search.
Thanks, :thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.