Forum Moderators: open
We have recently set up a phone list search (by surname) on our intranet. The code we have works fine and finds the right people. However if you mistype the surname or there are no records to display, the browser hangs indefinitely and consequently hangs the machine. Obviously this isn't satisfactory!
Basically what I'd like to know (being very new to ASP) is the best way to handle errors of this sort. Ideally I'd like to be able to return a "no results found" message on the page if the search failed to find a name. I've included the code we use (designed by a previous IT team) but if more info is required I'll post it accordingly and any help would be much appreciated.
Many thanks in advance
Richard
<%
Session.timeout = 1
If IsObject(Session("staff_conn")) Then
Set conn = Session("staff_conn")
Else
Set conn = Server.CreateObject("ADODB.Connection")
conn.open "staff","",""
Set Session("staff_conn") = conn
End If
%>
<%
' If IsObject(Session("CIMR_Staff_Search_by_Surname_rs")) Then
' Set rs = Session("CIMR_Staff_Search_by_Surname_rs")
' Else
sql = "SELECT [Phonelist].[Surname], [Phonelist].[First Name], [Phonelist].[Building Location], Phonelist.[Tel Extension], [Phonelist].[E-Mail Address 1] FROM Phonelist WHERE ((([Phonelist].[Surname])='" & Request.QueryString("[Enter Surname:]") & "')) ORDER BY [Phonelist].[Surname], [Phonelist].[First Name] "
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql, conn, 3, 3
If rs.eof Then
rs.AddNew
End If
Set Session("CIMR_Staff_Search_by_Surname_rs") = rs
' End If
%>
if so, do a check right at the top of your page:
<%
if request.querystring = "" then
response.write("No Results Found")
response.end
end if
%>
this will output the "No Results Found" error if your querystring contains no data. if there is data, then it will skip right over the check. let me know how you turn out.
However I was wanting to display "No result found" if there were no results to display following the surname search rather than if the original query string was empty.
Say for example I typed Smth instead of Smith the search should return no results and I wanted a message to reflect that.
Thanks for your help though,
Richard
If rs.eof Then
Response,write "No records found"
End If
Why the rs.addnew?
Also, as you probably know objects stored in the session is not a good idea. Is there any reason for it being done that way? ADO/ASP Scalability FAQ [support.microsoft.com]
That worked perfectly. I thought it might be a one liner. However as I said I didn't write this code and the guy that did left about a year ago I believe so as I'm VERY new to ASP I'm afraid I can't answer your questions regarding the rs.addnew and objects.
At the moment the code works so until I manage to get a bit more ASP savvy I'll stick to what I've got, then make the code a bit more acceptable!
Thank you very much for your help,
Richard
First of all, any chance you could pass the QueryString variable as simply "Surname"? Or where is that value coming from?
(e.g. [......]
'--start--
MySurName = Request.QueryString("Surname")
set rs = server.createobject("adodb.recordset")
sql = "select [Surname], [First Name] as FirstName, [Building Location] as Location,"
sql = sql & " [Tel Extension] as Extension, [E-Mail Address 1] as Email"
sql = sql & " FROM Phonelist"
sql = sql & " WHERE [Surname] = ' " & MySurName & " ' "
sql = sql & " ORDER BY [Surname], [First Name]"
rs.open sql, conn
if not rs.eof then
response.write "SurName: " & rs("Surname") & "<br>"
response.write "First Name: " & rs("FirstName") & "<br>"
response.write "Building Location: " & rs("Location") & "<br>"
response.write "Extension: " & rs("Extension") & "<br>"
response.write "Email Address: " & rs("Email") & "<br>"
else
response.write "No Records Found"
end if
'--end--
If you're not adding new records during this procedure then you wouldn't need rs.addnew.
Man I hate it when people put spaces in field names.