PDA

View Full Version : Trying to display first 3 records but only 1 record in database - stopping the error


murgeltd
06-02-2006, 01:47 PM
This scenario will probably not occur but if it does how can I stop the follow error from occuring.

I have a page that display the first 3 records from a table in date order. I have now added a condition on the SQL statement to say:

SELECT * FROM news WHERE newsLive = '0' ORDER BY newsPostedDate DESC

There is only one record that has newsLive value of 0 and as such it displays that record then the following error:

ADODB.Field error '800a0bcd'

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

I know it's because I have code that displays the first three records but there is only one. How do I test for smaller than 3 records without user getting above error.

If it helps my snipet of code is below

<% for i = 1 to 3 %>
<tr>
<td width="30%" valign="top">
<a href="newsDetails.asp?newsID=<%=(rs.Fields.Item("newsID").Value)%>"><% response.Write(rs("newsSubject")) %></a></br>
<p><% response.Write(rs("newsPostedDate")) %></p>
<!-- At the moment it creates new table for each entry - but need to create new td for each entry -->
</td>
</tr>
<%
rs.MoveNext
Next
%>

Cheers

miranda
06-02-2006, 02:19 PM
check for .EOF and if it occurs then simply use Exit For

<% for i = 1 to 3 %>
<tr>
<td width="30%" valign="top">
<a href="newsDetails.asp?newsID=<%=(rs.Fields.Item("newsID").Value)%>"><% response.Write(rs("newsSubject")) %></a></br>
<p><% response.Write(rs("newsPostedDate")) %></p>
<!-- At the moment it creates new table for each entry - but need to create new td for each entry -->
</td>
</tr>
<%
rs.MoveNext
If rs.EOF then Exit For
Next
%>

murgeltd
06-02-2006, 02:25 PM
BINGO! :thumbsup:

Thanks for your help.

I don't supose you have any other clues about my other posts??

http://www.codingforums.com/showthread.php?t=88195

http://www.codingforums.com/showthread.php?t=88177

Thanks again

Andy

degsy
06-06-2006, 01:25 PM
A better way would be to select 3 records then Do/While loop through your recordset instead of using a For loop.

If using Access then you can use TOP. If using mySQL you can use Limit.

ghell
06-06-2006, 08:17 PM
an even better way would be to use top/limit (dont know if theres a standard sql keyword for it, top also works in ms sql server among other things but limit is more useful for pulling a number of records that arent right at the top for example in paging, then, adodb.recordsets can do paging [im not sure how efficient they are at this, if they still pull all the data then they woudlnt be great]) then use getrows or getstring (in this case getstring would probably be most useful, and it is certainly fastest) to pull the data and close the connection before displaying it.