Display Records in a Table Horizontally

I want to display records in a table horizontally.
if number of records are even then it display correctly but if the number of records are odd then it gives error

Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record.

code is

<%do while not rst1.EOF%>
     <TD><%=rst1.Fields ("pic_id")%></TD>
    <%if not rst1.EOF then%>
    <%rst1.MoveNext%>
    <TD><%=rst1.Fields ("pic_id")%></TD>
    <%end if%>
<%     rst1.MoveNext
loop
%>
fiaz1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

answerbankCommented:
Hi,

I dont find the need of the if statement in your code. Try this code instead.
<table>
<tr>
<%while not rst1.eof%>
    <TD><%=rst1.Fields ("pic_id")%></TD>
    <%rst1.movenext%>
<%wend%>
</tr>
</table>
regards,
answerbank
fiaz1Author Commented:
Actually this is not an actual code but this is my requirement i used a lots of table in <td> tag .
turbosigCommented:
Let me know if you need more info, but use a loop and a counter.

Once you reach, X amount of records insert a </tr><tr> and start the process over.  Then when you hit an empty cell, i.e. your last row only has 3 cells, not for, insert an empty cell with the HTML code &nbsp;

<table><tr>
<%
do while not rst1.EOF
%>
x=1
For X = 1 to rst1.Count

if x=5 then
  </tr><tr>
   x=1
else  
<TD><%=rst1.Fields ("pic_id")%></TD>
end if
<%rst1.MoveNext%>
next

'You may want to consider .getrows here to build an array

'I have not tested this? I actually build an array.


Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

fiaz1Author Commented:
Actually this is not an actual code but this is my requirement i used a lots of table in <td> tag .
turbosigCommented:
Please re-phrase your second Q? you posted the same sentence twice.
Hornet241Commented:
Instead of this

   <%if not rst1.EOF then%>
   <%rst1.MoveNext%>
   <TD><%=rst1.Fields ("pic_id")%></TD>
   <%end if%>

try this

   <%if not rst1.EOF then exit do%>
   <%rst1.MoveNext%>
   <TD><%=rst1.Fields ("pic_id")%></TD>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fiaz1Author Commented:
here is the actual code

<div align="center">
      <center>
            <table border="0" cellpadding="0" cellspacing="0" height="87" align="center">
                  <tr>
                        <td rowspan="4" height="87" valign="bottom">
                              <table width="<%=session("framew1")%>" height="<%=session("frameh1")%>" border="0" cellpadding="0" cellspacing="0" align="center">
                                    <tr>
                                          <%if session("frame_id") then%>
                                                <td width="<%=session("framew")%>" height="<%=session("frameh")%>"><img src="<%=ltopc%>" width="<%=session("framew")%>" height="<%=session("frameh")%>"></td>
                                                <td width="<%=session("framew1")%>" height="<%=session("frameh")%>"><img src="<%=upper_f %>" width="<%=session("framew1")%>" height="<%=session("frameh")%>"></td>
                                                <td width="<%=session("framew")%>" height="<%=session("frameh")%>"><img src="<%=rtopc %>" width="<%=session("framew")%>" height="<%=session("frameh")%>"></td>
                                          <%end if%>
                                    </tr>
                                    <tr>
                                          <%if session("frame_id") then%>
                                                <td width="<%=session("framew")%>" height="<%=session("frameh1")%>"><img src="<%=lside %>" width="<%=session("framew")%>" height="<%=session("frameh1")%>"></td>
                                          <%end if%>
                                          <td align="center">
                                                <table background="<%=topmat_path%>" width="<%=session("topmatw")%>" height="<%=session("topmath")%>" border="0" cellpadding="0" cellspacing="0" align="center">
                                                      <tr><td align="center">
                                                            <table background="<%=midmat_path%>" width="<%=session("midmatw")%>" height="<%=session("midmath")%>" border="0" cellpadding="0" cellspacing="0" align="center">
                                                                  <tr><td align="center">
                                                                        <table background="<%=botmat_path%>" width="<%=session("botmatw")%>" height="<%=session("botmath")%>" border="0" cellpadding="0" cellspacing="0" align="center">
                                                                              <tr><td align="center">
                                                                                    <img src="<%=path_photo%>" width="<%=picw%>" height="<%=pich%>" align="center">
                                                                              </td></tr>
                                                                        </table>
                                                                  </td></tr>
                                                            </table>
                                                      </td></tr>
                                                </table>
                                          </td>
                                          <%if session("frame_id") then%>
                                                <td width="<%=session("framew")%>" height="<%=session("frameh1")%>"><img src="<%=rside %>" width="<%=session("framew")%>" height="<%=session("frameh1")%>"></td>
                                          <%end if%>
                                    </tr>
                                    <tr>
                                          <%if session("frame_id") then%>
                                                <td width="<%=session("framew")%>" height="<%=session("frameh")%>"><img src="<%=lbotc %>" width="<%=session("framew")%>" height="<%=session("frameh")%>"></td>
                                                <td width="<%=session("framew1")%>" height="<%=session("frameh")%>"><img src="<%=lower_f%>" width="<%=session("framew1")%>" height="<%=session("frameh")%>"></td>
                                                <td width="<%=session("framew")%>" height="<%=session("frameh")%>"><img src="<%=rbotc %>" width="<%=session("framew")%>" height="<%=session("frameh")%>"></td>
                                          <%end if%>
                                    </tr>
                              </table>
                              <td height="36"> </td>
                              <%if rst1.EOF then
                              rst1.MoveNext

'some asp code that get value in variables
%>
 %>
                              <td rowspan="4" height="87" valign="bottom">
                                    <table width="<%=session("framew1")%>" height="<%=session("frameh1")%>" border="0" cellpadding="0" cellspacing="0" align="center">
                                          <tr>
                                                <%if session("frame_id") then%>
                                                      <td width="<%=session("framew")%>" height="<%=session("frameh")%>"><img src="<%=ltopc%>" width="<%=session("framew")%>" height="<%=session("frameh")%>"></td>
                                                      <td width="<%=session("framew1")%>" height="<%=session("frameh")%>"><img src="<%=upper_f %>" width="<%=session("framew1")%>" height="<%=session("frameh")%>"></td>
                                                      <td width="<%=session("framew")%>" height="<%=session("frameh")%>"><img src="<%=rtopc %>" width="<%=session("framew")%>" height="<%=session("frameh")%>"></td>
                                                <%end if%>
                                          </tr>
                                          <tr>
                                                <%if session("frame_id") then%>
                                                      <td width="<%=session("framew")%>" height="<%=session("frameh1")%>"><img src="<%=lside %>" width="<%=session("framew")%>" height="<%=session("frameh1")%>"></td>
                                                <%end if%>
                                                <td align="center">
                                                      <table background="<%=topmat_path%>" width="<%=session("topmatw")%>" height="<%=session("topmath")%>" border="0" cellpadding="0" cellspacing="0" align="center">
                                                            <tr><td align="center">
                                                                  <table background="<%=midmat_path%>" width="<%=session("midmatw")%>" height="<%=session("midmath")%>" border="0" cellpadding="0" cellspacing="0" align="center">
                                                                        <tr><td align="center">
                                                                              <table background="<%=botmat_path%>" width="<%=session("botmatw")%>" height="<%=session("botmath")%>" border="0" cellpadding="0" cellspacing="0" align="center">
                                                                                    <tr><td align="center">
                                                                                          <img src="<%=path_photo%>" width="<%=picw%>" height="<%=pich%>" align="center">
                                                                                    </td></tr>
                                                                              </table>
                                                                        </td></tr>
                                                                  </table>
                                                            </td></tr>
                                                      </table>
                                                </td>
                                                <%if session("frame_id") then%>
                                                      <td width="<%=session("framew")%>" height="<%=session("frameh1")%>"><img src="<%=rside %>" width="<%=session("framew")%>" height="<%=session("frameh1")%>"></td>
                                                <%end if%>
                                          </tr>
                                          <tr>
                                                <%if session("frame_id") then%>
                                                      <td width="<%=session("framew")%>" height="<%=session("frameh")%>"><img src="<%=lbotc %>" width="<%=session("framew")%>" height="<%=session("frameh")%>"></td>
                                                      <td width="<%=session("framew1")%>" height="<%=session("frameh")%>"><img src="<%=lower_f%>" width="<%=session("framew1")%>" height="<%=session("frameh")%>"></td>
                                                      <td width="<%=session("framew")%>" height="<%=session("frameh")%>"><img src="<%=rbotc %>" width="<%=session("framew")%>" height="<%=session("frameh")%>"></td>
                                                <%end if%>
                                          </tr>
                                    </table>
                              </td>
                         <%end if%>
                  </tr>
            </table>
      </center>
</div>
<%
rst1.MoveNext
loop
%>

turbosigCommented:
Holy COW.. You are using more session objects than I have ever seen.  Ever thought of creating an array and storing that in the object?  Better that having 20+ session objects created??

Also, I see <-- rst1.MoveNext --> at the end of your code although I do not see a single reference to any DB fields.

If this is the actual code, I believe you are getting confused with oRS("xValue") and Session("xValue").

oRS("xValue") - Record set value

Session("xValue") - server side object

Also, why do you use dynamic values like (rside) for your images? do they change often?

Please advise if you have actually USED this code in production, to me it looks as if you would need 1 gig of memory to keep this baby going?
fiaz1Author Commented:
This is not complete code, DB fields are set above than this code. Actual requirment is I display dynamic images in horizantol format just like this
1      2
3      4
....

so for this we move cursor to next records in a do while loop.
let say

do while not rs.eof
display first record  rs.movenext()(here we move cursor to next record) display second record
rs.movenext
loop
(now at the end of loop we again move cursor to next record.)

the output is like this
1     2
3     4

but when the records are odd then problem occur.
 
fiaz1Author Commented:
This is not complete code, DB fields are set above than this code. Actual requirment is I display dynamic images in horizantol format just like this
1      2
3      4
....

so for this we move cursor to next records in a do while loop.
let say

do while not rs.eof
display first record  rs.movenext()(here we move cursor to next record) display second record
rs.movenext
loop
(now at the end of loop we again move cursor to next record.)

the output is like this
1     2
3     4

but when the records are odd then problem occur.
 
fiaz1Author Commented:
This is not complete code, DB fields are set above than this code. Actual requirment is I display dynamic images in horizantol format just like this
1      2
3      4
....

so for this we move cursor to next records in a do while loop.
let say

do while not rs.eof
display first record  rs.movenext()(here we move cursor to next record) display second record
rs.movenext
loop
(now at the end of loop we again move cursor to next record.)

the output is like this
1     2
3     4

but when the records are odd then problem occur.
 
turbosigCommented:
Do what you are doing to build an array, then use a For - Next Loop and build the table.  If there is only 1 image on the row and the last array slot is empty, then just insert an blank <td>.
fiaz1Author Commented:
i think u do not understand my question.
problem in recordset not in table.
fiaz1Author Commented:
code is here

<%do while not rs.eof%>
<tr>
'first record
<td><%=rs("name")%></td>
<%rs.movenext%>
'second record
<td><%=rs("name")%></td>
</tr>
'for third record
<%rs.movenext
loop
%>

so if records are even then its ok
but if records are odd then first rs.movenext genrates error
"either end or before of files or records deleted"
Hornet241Commented:
Add this

    <%do while not rs.eof%>
      <tr>
      'first record
        <td><%=rs("name")%></td>
        <%rs.movenext%>
*** If rs.eof Then Exit Do ****
        'second record
        <td><%=rs("name")%></td>
      </tr>
        'for third record
        <%rs.movenext
    loop%>

minus the ***'s

this will exit the loop if an EOF occurs in between the records
hongjunCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
[split b/w turbosig(Assisted) and Hornet241(Accepted)]

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

hongjun
EE Cleanup Volunteer
garnett-dickinsonCommented:
Hi - I'd been searchiong for a simple answer to the horizontal looper from DB to HTML table and came uop with this very simple version - just change the iRecordsPerRow to the amount of records you want and it should do the rest! - no messing with cursers and move next blah,blah - keepin it simple!!
            <%
			'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'..---...-.--...-.-..--- ...-.--...-.-..---..---
'_____HORIZONTAL LOOPER - _____HORIZONTAL LOOPER - 			
	''Open the table
	Response.Write 	"<table width='1000' border='1' cellspacing='0' cellpadding='0'>"
	'Open the first row
	Response.Write "<TR>"	
	
	Dim iLoop 'Create a counter.
	iLoop = 0 'Set the counter to 1
	Dim iRecordsPerRow 
	''..---...-.--...-.-..--- 
	iRecordsPerRow = 4'Set this to the amount of columns you want in the table row
	Dim strRowFinisher 'Unless you fall lucky, you will need some <TD></TD> tags at the end of the last row to make the table correctly
	strRowFinisher = ""
	Dim iBackCount 'iBackCount takes the amount per row and subtracts one on each loop
	iBackCount = iRecordsPerRow

	'Create a recordset
	Set objRS = Server.CreateObject("ADODB.Recordset")
	
	'Open it
	objRS.Open strSQL, objConn
	
	'Start pouring the records out
	Do WHile Not objRS.EOF
	
	Response.Write "<td>" &  objRS("Product_ID") & "</td>"

	'Increment the loop by one
  	iLoop = (iLoop + 1)	
	iBackCount = (iBackCount - 1)
 		If (iLoop = iRecordsPerRow) Then 'We are at the end of the row so at the TR end of row and start of row
			Response.Write "</TR><TR>"
			iLoop = 0 'So reset the counter to 1
			iBackCount = iRecordsPerRow 'Reset iBackCount to the amount of records in a row
		End If
  	objRS.MoveNext
	Loop
	
	'Finish off the last row with the right amount of TD tage to complete the row
	'If iLoop is on zero this doesn';t need to happen
	If (iLoop <> 0) Then 'Do it
	Dim iCount 'Create a new counter
	For iCount = 1 To iBackCount 'Go from 1 to the amount generated by iBackCount
	strRowFinisher = (strRowFinisher & "<TD></TD>")
	'EG If we only have 2 records on the last row, iBackCount will = 3 and the strRowFinisher will be three sets of TD tags
	next
	Response.Write strRowFinisher
	End If
	
	'Write the end of the final row
	Response.Write "</TR>"
	'Close the table
	Response.Write "</TABLE>"

'END HORIZONTAL LOOPER - END HORIZONTAL LOOPER - END	'_________________________________________________________________'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  %>   

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.