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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to ASP--passing parameter to ASP page and declaring defaults

Status
Not open for further replies.
Jan 14, 2003
194
US
I created some pages in FrontPage and I wanted to change some things, but just like it does with regular HTML, FrontPage seems to bloat everything and the ASP isn't what I would expect it to be.

I had an ASP page that used a single drop-down box populated from a column in my database, then used the selection in that drop-down to show the results below--not on a 2nd page.

With the FrontPage version, I had the SQL string with two parameters WHERE (field = '::param1::') ORDER BY ::param2::

The page would default to displaying no records and would say so just below the table header row ("No matching records").

I have re-written the page by hand but am having trouble with those two parameters. Since no defaults are specified (I don't know how to do this) and also since I don't have any code in the database that returns a custom error message, when I try to open the page I receive:

ADODB.Recordset error '800a0bcd'

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

/misc/asp/testsearh.asp, line 23

So, my questions are as follows:
1. How do I specify defaults for the two parameters so that the page comes up properly and allows me to use the drop-down box to make a selection and have the results display below it?
2. How do I tell the page to display "No matching records" if BOTH the parameter is missing or if the selection made in the drop-down box produces no results?

Here's the page code:
Code:
<html>

<head>
<title>Genre Search</title>
</head>

<body>

<%
dim oRSgenres
dim oRSqrydvdlistbygenre

set oRSGenres = Server.CreateObject("ADODB.Recordset")
set oRSqrydvdlistbygenre = Server.CreateObject("ADODB.Recordset")

sqlGenres = "SELECT * FROM tblGenres ORDER BY genre ASC;"
sqlqrydvdlistbygenre = "SELECT * FROM qrydvdlistbygenre WHERE genre = 'Foreign' ORDER BY title ASC;"

oRSgenres.open sqlGenres, "DSN=dvdlist"
oRSgenres.MoveFirst

oRSqrydvdlistbygenre.open sqlqrydvdlistbygenre, "DSN=dvdlist"
oRSqrydvdlistbygenre.MoveFirst
%>

<form method="get" action="testgenresearch.asp" onsubmit="return frmsearchgenre_Validator(this)" name="frmsearchgenre">
 <b><font size="2">Choose a Genre</font></b><br>
 <select name="selgenre" onchange=frmsearchgenre.submit() size="1">
 <option selected value=""><%Request.Form("genre")%></option>
      <%
      Do While NOT oRSgenres.EOF
    	Response.Write "<option value='" & oRSgenres("genreID") & "'>" & oRSgenres("genre") & "</option>"
        oRSgenres.MoveNext
      Loop
      %>
 </select>
</form>

<i><font size="2">See more detailed movie information at <a target="_blank" href="[URL unfurl="true"]http://www.allmovie.com">All[/URL] Movie Guide</a></font></i>

<br><br>

<table width="100%" border="0" cellpadding="0" cellspacing="0" height="74">
  <th>
    <tr>
      <td bgcolor="#4E6BA3" width="243" height="19"><b><font color="#FFFFFF">Title</font></b></td>
      <td bgcolor="#4E6BA3" width="225" height="19"><b><font color="#FFFFFF">Notes</font></b></td>
      <td bgcolor="#4E6BA3" width="178" height="19"><b><font color="#FFFFFF">Genres</font></b></td>
      <td bgcolor="#4E6BA3" width="51" height="19"><b><font color="#FFFFFF">Year</font></b></td>
      <td bgcolor="#4E6BA3" width="51" height="19"><b><font color="#FFFFFF">In</font></b></td>
    </tr>
  </th>

<% 
  Do While NOT oRSqrydvdlistbygenre.EOF
%>

<%
  Dim x, varbgcolor
  if x = 1 then
    varbgcolor="#C0C0C0"  
    x=2
  Else
    varbgcolor="#FFFFFF"
    x=1
  End if
%>

  <tr>
    <td bgcolor="<%=varbgcolor%>" width="243"><font size="2"><b><%=oRSqrydvdlistbygenre("title")%></b></font></td>
    <td bgcolor="<%=varbgcolor%>" width="225"><font size="2"><i><%=oRSqrydvdlistbygenre("notes")%></i></font></td>
    <td bgcolor="<%=varbgcolor%>" width="178"><font size="2"><%=oRSqrydvdlistbygenre("genres")%></font></td>
    <td bgcolor="<%=varbgcolor%>" width="51"><font size="2"><%=oRSqrydvdlistbygenre("year")%></font></td>
    <td bgcolor="<%=varbgcolor%>" width="51"><font size="2"><%=oRSqrydvdlistbygenre("year")%></font></td>
  </tr>
<%
  oRSqrydvdlistbygenre.MoveNext
  Loop
%>

</table>

</body>

</html>
 
Correction to the code.
The 2nd sqltxt line should read:
Code:
sqlqrydvdlistbygenre = "SELECT * FROM qrydvdlistbygenre WHERE genre = 'Foreign' ORDER BY title ASC;"
 
DANG! I forgot to correct it AGAIN! Here goes:
Code:
sqlqrydvdlistbygenre = "SELECT * FROM qrydvdlistbygenre WHERE (genre = '::genre::') ORDER BY ::title:: ASC;"
 
I guess you got wrong codes manner there. [lipstick2]

Code:
oRSgenres.open sqlGenres, "DSN=dvdlist"
oRSgenres.MoveFirst [red]-->as far as I know when you open recordset, then the cursor always stay in the first line (if there's returnable record) OR stay in BOF (if there's no returnable record)  [/red]

oRSqrydvdlistbygenre.open sqlqrydvdlistbygenre, "DSN=dvdlist"
oRSqrydvdlistbygenre.MoveFirst [red]--> same explanation as above  [/red]

So Better do like this :
Code:
<body>
<%
dim oRSgenres
dim oRSqrydvdlistbygenre

set oRSGenres = Server.CreateObject("ADODB.Recordset")
set oRSqrydvdlistbygenre = Server.CreateObject("ADODB.Recordset")

sqlGenres = "SELECT * FROM tblGenres ORDER BY genre ASC;"
sqlqrydvdlistbygenre = "SELECT * FROM qrydvdlistbygenre WHERE genre = 'Foreign' ORDER BY title ASC;"
%>

<form method="get" action="testgenresearch.asp" onsubmit="return frmsearchgenre_Validator(this)" name="frmsearchgenre">
 <b><font size="2">Choose a Genre</font></b><br>
<%  
oRSgenres.open sqlGenres, "DSN=dvdlist"
If Not oRSgenres.BOF then
%>
 <select name="selgenre" onchange=frmsearchgenre.submit() size="1">
 <option selected value=""><%Request.Form("genre")%></option>
      <%
      Do While NOT oRSgenres.EOF
        Response.Write "<option value='" & oRSgenres("genreID") & "'>" & oRSgenres("genre") & "</option>"
        oRSgenres.MoveNext
      Loop
      %>
 </select>
<%
Else   'No returnable record
   response.write "Currently No Matched Records"
End If
oRSgenres.Close   [red]'always remember to close your recordset[/red]
set oRSgenres = nothing    [red]'release your object for sure, to loosen you server cache[/red]
%>
</form>

<i><font size="2">See more detailed movie information at <a target="_blank" href="[URL unfurl="true"]http://www.allmovie.com">All[/URL] Movie Guide</a></font></i>

<br><br>

<table width="100%" border="0" cellpadding="0" cellspacing="0" height="74">
  <th>
    <tr>
      <td bgcolor="#4E6BA3" width="243" height="19"><b><font color="#FFFFFF">Title</font></b></td>
      <td bgcolor="#4E6BA3" width="225" height="19"><b><font color="#FFFFFF">Notes</font></b></td>
      <td bgcolor="#4E6BA3" width="178" height="19"><b><font color="#FFFFFF">Genres</font></b></td>
      <td bgcolor="#4E6BA3" width="51" height="19"><b><font color="#FFFFFF">Year</font></b></td>
      <td bgcolor="#4E6BA3" width="51" height="19"><b><font color="#FFFFFF">In</font></b></td>
    </tr>
  </th>

<% 
oRSqrydvdlistbygenre.open qlqrydvdlistbygenre, "DSN=dvdlist"
If Not oRSqrydvdlistbygenre.BOF then  [purple]'why using this kind of long variable name, very confusing[/purple]

  Do While NOT oRSqrydvdlistbygenre.EOF
%>

<%
  Dim x, varbgcolor
  if x = 1 then
    varbgcolor="#C0C0C0"  
    x=2
  Else
    varbgcolor="#FFFFFF"
    x=1
  End if
%>

  <tr>
    <td bgcolor="<%=varbgcolor%>" width="243"><font size="2"><b><%=oRSqrydvdlistbygenre("title")%></b></font></td>
    <td bgcolor="<%=varbgcolor%>" width="225"><font size="2"><i><%=oRSqrydvdlistbygenre("notes")%></i></font></td>
    <td bgcolor="<%=varbgcolor%>" width="178"><font size="2"><%=oRSqrydvdlistbygenre("genres")%></font></td>
    <td bgcolor="<%=varbgcolor%>" width="51"><font size="2"><%=oRSqrydvdlistbygenre("year")%></font></td>
    <td bgcolor="<%=varbgcolor%>" width="51"><font size="2"><%=oRSqrydvdlistbygenre("year")%></font></td>
  </tr>
<%
  oRSqrydvdlistbygenre.MoveNext
  Loop

Else    'No matched record
  response.write "<tr><td colspan=""5"" height=""19"">Sorry, currently no record found</td></tr> "

End if
oRSqrydvdlistbygenre.close
Set oRSqrydvdlistbygenre = Nothing

%>

</table>
</body>
 
These questions probably betray my understanding but in any case.
1) On initial loading of the page, you populate the 'genre' option list by accessing the d/b with a default genre of 'foreign' as per the code:
sqlqrydvdlistbygenre = "SELECT * FROM qrydvdlistbygenre WHERE genre = 'Foreign' ORDER BY title ASC;"
True or false

2) When does Request.Form("genre") get populated. I am referring to the line of code:
<option selected value=""><%Request.Form("genre")%></option>

Thanks

 
I corrected some stuff. The form submission wasn't working because I was using "get" instead of "post" and the variables were being passed in the URL instead.

JJ26, thanks for the advice. I'm still really new at this, but the fog is clearing slowly. I have closed all of me recordsets and released the objects when done. I had created the object with such a long name because it matched the name of my query in the database, but I shortened it because it was ridiculously long and tedious to type (too much room for possible errors).

shaunk, as for your questions, I have changed that coding. I'll paste the amended code below and some follow-up questions now that the page is working. Request.Form("genre") gets populated in that code referrence on the initial page load with nothing, so that the first option in my select input is blank. Then, after choosing a genre to search, when the page refreshed displaying the new data, the genre that was chosen remains in the box so that I don't have to print the selected genre on the screen.

You can the page in question here:

It's hosted on my home server with the web service bandwidth throttled back so it's pretty slow, but it's great for testing and developing.

My new questions are as follows:
1. When the page loads, the genre box doesn't produce an error on the page, however the Sort By box does. Since I want it to do the same thing as the genre box by displaying the selection after the page loads the data, I set it selected to Request.Form("sort"). This causes the ORDER BY clause to produce an error since nothing is selected at first. Is there a way to correct this? This gets back to my original questions abotu setting defaults for parameters that are overridden by any new selections made.
2. How do I get the selected checkbox to show the last selection when after the form submit?
3. Relating to question 2, I'm thinking I'd need an IF Then...Else statement to tell the page NOT to process any of the data in the results table until after a selection has been made on all three boxes. Something like:
Code:
If input boxes are Null Then (not sure how to word this)
  Response.Write "No matching records.  Please select a genre and sort criteria"
Else
  ' the rest of the code that processes the submitted data and displays the results
End If

Ok, here is the current page code (I've removed the javascript stuff for the animated text while the page loads:
Code:
<html>

<head>
<title>Genre Search</title>
</head>

<body>

<%
dim oRSgenres
dim oRSqrygenre

set oRSGenres = Server.CreateObject("ADODB.Recordset")
set oRSqrygenre = Server.CreateObject("ADODB.Recordset")

sqlGenres = "SELECT * FROM tblGenres ORDER BY genre ASC;"
sqlqrygenre = "SELECT * FROM qrydvdlistbygenre WHERE genre = '" & Request.Form("selgenre") & "' ORDER BY " & Request.Form("sort") & " " & Request.Form("order") & ";"

oRSgenres.open sqlGenres, "DSN=dvdlist"
%>

<form method="post" action="searchgenre.asp" onsubmit="return frmsearchgenre_Validator(this)" name="frmsearchgenre">
 <table border="0" cellpadding="0" cellspacing="0">
   <tr>
     <td colspan="2">
         <b><font size="2">Choose a Genre</font></b><br>
         <select name="selgenre" size="1" tabindex="1">
           <option selected><%=Request.Form("selgenre")%></option>
             <%
             Do While NOT oRSgenres.EOF
               Response.Write "<option value='" & oRSgenres("genre") & "'>" & oRSgenres("genre") & "</option>"
               oRSgenres.MoveNext
             Loop
             
             oRSgenres.close
             set oRSgenres = nothing
             %>
         </select>
     </td>
   </tr>
   <tr>
     <td>
       <b><font size="2">Sort By</font></b><br>
       <select name="sort" size="1"  tabindex="2">
         <option selected value=""><%=Request.Form("sort")%></option>
         <option value="Title">Title</option>
         <option value="Notes">Notes</option>
         <option value="Genres">Genres</option>
         <option value="Year">Year</option>
       </select>
     </td>
     <td>
       &nbsp;<input type="radio" name="order" value="ASC" checked tabindex="3"><font size="2"><i>Ascending</i></font><br>
       &nbsp;<input type="radio" name="order" value="DESC" tabindex="4"><font size="2"><i>Descending</i></font>
     </td>
   </tr>
 </table>
 <br>
 <input type="submit" value="Show Movies" name="submit">
</form>
<i><font size="2">See more detailed movie information at <a target="_blank" href="[URL unfurl="true"]http://www.allmovie.com">All[/URL] Movie Guide</a></font></i>
<br>
<table width="100%" border="0" cellpadding="0" cellspacing="0" height="74">
  <th>
    <tr>
      <td bgcolor="#4E6BA3" width="243" height="19"><b><font color="#FFFFFF">Title</font></b></td>
      <td bgcolor="#4E6BA3" width="225" height="19"><b><font color="#FFFFFF">Notes</font></b></td>
      <td bgcolor="#4E6BA3" width="178" height="19"><b><font color="#FFFFFF">Genres</font></b></td>
      <td bgcolor="#4E6BA3" width="51" height="19"><b><font color="#FFFFFF">Year</font></b></td>
      <td bgcolor="#4E6BA3" width="51" height="19"><b><font color="#FFFFFF">In</font></b></td>
    </tr>
  </th>

<% 
  oRSqrygenre.open sqlqrygenre, "DSN=dvdlist"
  
  Do While NOT oRSqrygenre.EOF
  
    Dim x, varbgcolor
    if x = 1 then
      varbgcolor="#C0C0C0"  
      x=2
    Else
      varbgcolor="#FFFFFF"
      x=1
    End if
%>
  <tr>
    <td bgcolor="<%=varbgcolor%>" width="243"><font size="2"><b><%=oRSqrygenre("title")%></b></font></td>
    <td bgcolor="<%=varbgcolor%>" width="225"><font size="2"><i><%=oRSqrygenre("notes")%></i></font></td>
    <td bgcolor="<%=varbgcolor%>" width="178"><font size="2"><%=oRSqrygenre("genres")%></font></td>
    <td bgcolor="<%=varbgcolor%>" width="51"><font size="2"><%=oRSqrygenre("year")%></font></td>
    <td bgcolor="<%=varbgcolor%>" width="51"><font size="2"><%=oRSqrygenre("In")%></font></td>
  </tr>
<%
  oRSqrygenre.MoveNext
  Loop  

  oRSqrygenre.close
  set oRSqrygenre = nothing
%>

</table>

</body>

</html>
 
To save your server processor, maybe you can put some validation (using client script, of course) before Users click the button show DVD.
As to examine the value which Users would choose in those of your combo boxes & radio buttons.

If the users haven't been set things right, then your Javascript or VB script can give warnings againts the error or incompleteness.
It would be better to do this (I think) 'cause users won't have to connect to your server just to know that his search criteria is not in proper value.

When you submit the form, then you can get the value of your radio button object by accessing the request.form function. there's different between check box & radio button; be careful.
Code:
<!-- You can use ASP snippet, to check your previous value of your radio button -->

<input type="radio" name="order" value="ASC" 
<% If Request.Form("order") = "ASC" then  response.write "checked"  %> tabindex="3"><font size="2"><i>Ascending</i></font>

Ciao...!~~~~ [lipstick2]
Have a nive weekend there...
 
Every time I start to work on something it seems to open up a whole other can of worms that I need to get a grasp on before finishing what I started. I wound up not needing any validation since the genre drop-down is populated from a table field (all options are valid), the Sort By drop-down is a static set of options so they're all valid, and I used some javascript to disable the radio buttons until a valid option was chosen in the Sort By drop-down, so that if you leave it blank to use the query default, the SQL qeury doesn't look like "WHERE [some stuff] ASC" (missing the ORDER BY clause which the Sort By drop-down adds).

Everything works really well now.

Thanks for the tips guys! I know I'll have tons more questions as I start re-doing all of the pages I created in FrontPage by hand.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top