Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exception Occurred Error 1

Status
Not open for further replies.

kefi2927

IS-IT--Management
Apr 3, 2003
40
GB
Hi
I have a page the uses a form the another page calls fields from that form. This is to allow the user to search for a particular record.
Want to redirect user to another page if field doesn't match with access db field

'LastName field from other page
searchTerm=Request.Form("LastName")

'Thought this might work but doesn't
If searchTerm <> RS(&quot;LastName&quot;) Then
Response.Redirect(&quot;admin.asp&quot;)
End If

If LastName field on form is left empty then user is redirected.
If user types in a name that exists in acces db then user is still redirected !!
If user types in name that doesn't exist in access db then &quot;Error '80020009'Exception Occurred&quot; appears on page.
If the IF Then statement is taken out of page the page works fine but doesn't allow for error by user.

Has anyone any ideas. Your help would be greatly appreciated. Cheers
 
Exception occured because you tried to access a field in a recordset, but the recordset doesnt exist because no matching records were found. You need to check for .EOF before trying anything else e.g.

If Not RS.EOF then
If searchTerm <> RS(&quot;LastName&quot;) Then
Response.Redirect(&quot;admin.asp&quot;)
End If
End If

The above handles the fact that a name is found and doesnt redirect so...

If Not RS.EOF then
If searchTerm <> RS(&quot;LastName&quot;) Then
Response.Redirect(&quot;admin.asp&quot;)
End If
Else
' perform your code
End if
 
It may be a little cleaner to do something like this:
Code:
If Request.Form(&quot;LastNamehing&quot;) = &quot;&quot; Then
   Response.Redirect &quot;some page&quot;
End If

'do your db connection here:
'   &quot;select from table where lastname LIKE '&quot;&Request.Form(&quot;LastName&quot;) & &quot;'&quot;

If NOT rs.EOF Then
   'it would only be EOF if that name isn't in db already
   Response.Redirect &quot;some page&quot;
   Response.End
End If

'continue with code now, assured that the last name is positive length and doesn't exist in db already

Since your going to be Response.Redirect'ing it might be cleaner to just response.end after them and let the code later on not be stuck inside an else. The Response.End shouldn't even be necessary because thwe redirect makes it stop procwessing this page and shift to the othe page, I just though it would be cleaner to put them in.

The select statement selects for the last name given, that way if it comes back as EOF that person isn't in the db, if it comes back not EOF thanm it muct have at least one record and therefore the last name is already in the db.

-Tarwn 01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101
29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
Hi thanks for all the help. Much appreciated.
I tried Tarwn's code which works a treat until i try to put into the LastName form field something that doesn't exist in the record and instead of the redirection kicking in to go the the redirected page, it just displays the page that it supposes to display if you input something but its blank.

Don't know where to go to next with it.

Tried putting the following code in between the RS.MoveNext and Loop. Tried putting it after the Loop. Tried putting it before the Loop. All with the same effect.

If NOT rs.EOF Then
'it would only be EOF if that name isn't in db already
Response.Redirect &quot;admin.asp&quot;
Response.End
End If

Any ideas. Cheers
 
Hi
Some of the code. Hope this helps you to understand it. You'll have to forgive me if it doesn't as i'm new to ASP.

<%@LANGUAGE=&quot;VBSCRIPT&quot; CODEPAGE=&quot;1252&quot;%>

<!-- #include file = &quot;../includes/ADOVBS.INC&quot; -->
<!-- #include file = &quot;../includes/LOGINNOW.ASP&quot; -->
<!--#include file=&quot;../includes/connection.asp&quot; -->
<%
Response.Buffer = True
' Declare variables

Dim Conn
Dim RS
Dim SQL
Dim searchTerm

If Request.Form(&quot;LastName&quot;) = &quot;&quot; Then
Response.Redirect &quot;admin.asp&quot;
End If


'create connection object
Set Conn=Server.CreateObject(&quot;ADODB.Connection&quot;)
Conn.Open Openvar

searchTerm=Request.Form(&quot;LastName&quot;)


'create recordset object
Set RS=Server.CreateObject(&quot;ADODB.RecordSet&quot;)

'set and assign structured query statement to variable
SQL=&quot;SELECT * FROM Employees WHERE Employees.LastName LIKE '&quot;&Request.Form(&quot;LastName&quot;) & &quot;' ORDER BY LastName&quot;

'opens the table to view all records and open and lock modes
RS.Open SQL, Conn

%>

<%
Do Until RS.EOF 'keep going until the end
%>

<tr>
<td class=&quot;dataContent&quot;> <%=RS(&quot;CentreName&quot;)%></td>
<td class=&quot;dataContent&quot;> <a href=&quot;detail_employee.asp?EmployeeID=<%=RS(&quot;EmployeeID&quot;)%>&quot;><%=RS(&quot;LastName&quot;)%></a></td>
<td class=&quot;dataContent&quot;> <%=RS(&quot;FirstName&quot;)%></td>
<td class=&quot;dataContent&quot;> <%=RS(&quot;Title&quot;)%></td>
</tr>


<%

RS.MoveNext 'Move to next record

If NOT rs.EOF Then
'it would only be EOF if that name isn't in db already
Response.Redirect &quot;admin.asp&quot;
Response.End
End If

Loop 'Repeat the code

%>

Thanks for any help. Cheers
 
Just changed slightly

SQL=&quot;SELECT * FROM Employees WHERE LastName LIKE '&quot;&Request.Form(&quot;LastName&quot;) & &quot;' ORDER BY LastName&quot;

RS.Open SQL, Conn

If Not RS.Eof then
Do Until RS.EOF %>
<tr>
<td class=&quot;dataContent&quot;> <%=RS(&quot;CentreName&quot;)%></td>
<td class=&quot;dataContent&quot;> <a href=&quot;detail_employee.asp?EmployeeID=<%=RS(&quot;EmployeeID&quot;)%>&quot;><%=RS(&quot;LastName&quot;)%></a></td>
<td class=&quot;dataContent&quot;> <%=RS(&quot;FirstName&quot;)%></td>
<td class=&quot;dataContent&quot;> <%=RS(&quot;Title&quot;)%></td>
</tr><%
RS.MoveNext
Loop
Else
Response.Redirect &quot;admin.asp&quot;
Response.End
End If
conn.close
rs.close
set conn=nothing
set rs=nothing
 
Hi GaryC123

Your code works. What a load off hwne you've got a manager on your back just wamting you to get it working !! Thanks for your time and help.

Take care
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top