display a message or redirect if the recordset is empty

 

if you use a filter criterium that makes it possible that no records are retrieved you should always put a "Hide Region (If Empty)" or a "Repeat Region" behaviour around the display of the database content. otherwise you might get an error

"Either EOF is true or BOF..."

with these behaviours the page simply displays nothing if the recordset is empty.

but sometimes you might want to display a message if the recordset is empty

 

here is one way of how to do it with VBScript:

NOTE: what you have to enter manually is only the bold part! The rest can be done using the standard tools.

a hide region (if empty) code generated with the Ultradev databindings and server behaviours would look like the following:

<% If Not Recordset1.EOF Or Not Recordset1.BOF Then %>
<%=(Recordset1.Fields.Item("myField").Value)%>
<% End If ' end Not Recordset1.EOF Or NOT Recordset1.BOF %>

all you have to do is to enter two additional lines:

<% If Not Recordset1.EOF Or Not Recordset1.BOF Then %>
<%=(Recordset1.Fields.Item("myField").Value)%>
<% Else %>
<%= "No record found!" %>

<% End If ' end Not Recordset1.EOF Or NOT Recordset1.BOF %>

 

a repeat region generated with the Ultradev tools would look like the following:

<%
While ((Repeat1__numRows <> 0) AND (NOT Recordset1.EOF))
%>

<%=(Recordset1.Fields.Item("myField").Value)%>


<%
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
Recordset1.MoveNext()
Wend
%>


an easy way to display the message is to add some new script after that so that the overal script would look like:

<%
While ((Repeat1__numRows <> 0) AND (NOT Recordset1.EOF))
%>

<%=(Recordset1.Fields.Item("myField").Value)%>


<%
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
Recordset1.MoveNext()
Wend
%>
<%
If (Recordset1.EOF or Recordset1.BOF) then
Response.Write("No records found!")
End if
%>


you could use this method also in conjunction with the "HideRegion" behaviour.

if you rather redirect the user to another page then use code like the following:

<%
If (Recordset1.EOF or Recordset1.BOF) then
Response.Redirect("mypage.asp")
End if
%>

 

 

here is the same for JavaScript:

AGAIN: what you have to enter manually is only the bold part! The rest can be done using the standard tools.

hide region (if empty):

<% if (!Recordset1.EOF || !Recordset1.BOF){ %>
<%=(Recordset1.Fields.Item("myField").Value)%>
<% } // end !Recordset1.EOF || !Recordset1.BOF %>
<% else { %>
<%= "No records found" %>

<% } %>

repeat region:

<%
while ((Repeat1__numRows-- != 0) && (!Recordset1.EOF))
{
%>

<%=(Recordset1.Fields.Item("myField").Value)%>


<%
Repeat1__index++;
Recordset1.MoveNext();}
%>

<% if (Recordset1.EOF || Recordset1.BOF) {
Response.Write("No records found");
} %>

 

or for redirecting

<% if (Recordset1.EOF || Recordset1.BOF) {
Response.Redirect("myPage.asp");
} %>

 

1