ASP 101 - Active Server Pages 101 - Web01
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash





ASP Source Code:
<%
' Declare variables
Dim strURL      ' The URL of this page
Dim cnnSearch   ' ADO connection
Dim rstSearch   ' ADO recordset
Dim strSQL      ' The SQL Query we build on the fly
Dim strSearch   ' The text being searched for
Dim arrTerms    ' An array of the terms in the search query
Dim strOperator ' Are we using AND or OR
Dim I           ' Looping var
' Retreive the URL of this page from Server Variables
strURL = Request.ServerVariables("URL")
' Retreive the list of terms being searched for.
strSearch = Request.QueryString("search")
' FIgure out if the user requested an "AND" which only returns
' results which match all terms or an "OR" which returns results
' which match any term.
If LCase(Request.QueryString("operator")) = "and" Then
	strOperator = "AND"
Else
	strOperator = "OR"
End If
%>
<p>Search our sample db by first or last name.  (% returns all)</p>
<form action="<%= strURL %>" method="get">
<input name="search" value="<%= strSearch %>" />
<select name="operator">
	<option value="or" <% If strOperator="OR"  Then Response.Write(" selected=""selected""")%>>Any Term</option>
	<option value="and"<% If strOperator="AND" Then Response.Write(" selected=""selected""")%>>All Terms</option>
</select>
<input type="submit" />
</form>
<p>[Try 'er in' for an example]</p>
<%
If strSearch <> "" Then
	' Create an ADO Connection to connect to the sample database.
	Set cnnSearch = Server.CreateObject("ADODB.Connection")
	' This line is for the Access sample database:
	'cnnSearch.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database.mdb") & ";"
	' We're actually using SQL Server so we use this line instead:
	cnnSearch.Open "Provider=SQLOLEDB;Data Source=10.2.2.133;" _
		& "Initial Catalog=samples;User Id=samples;Password=password;" _
		& "Connect Timeout=15;Network Library=dbmssocn;"
	' Double up single quotes
	strSearch = Replace(strSearch, "'", "''")
	' Split search phrase into individual terms
	arrTerms = Split(strSearch, " ")
	' Build our query based on the input.
	strSQL = "SELECT last_name, first_name, sales FROM [sample] WHERE "
	' Loop over terms including each one.
	For I = LBound(arrTerms) to UBound(arrTerms)
		' I'm searching both first and last name fields so
		' I need to do both for each search term.
		strSQL = strSQL & "(   last_name  LIKE '%" & arrTerms(I) & "%'"
		strSQL = strSQL & " OR first_name LIKE '%" & arrTerms(I) & "%')"
		' Use the appropriate operator to combine the individual search terms
		If I < UBound(arrTerms) Then 
			strSQL = strSQL & " " & strOperator & " "
		End If
	Next 'I
	' Order by last_name
	strSQL = strSQL & " ORDER BY last_name;"
	'Response.Write strSQL
	' Execute the query
	Set rstSearch = cnnSearch.Execute(strSQL)
	' Display the results
	%>
	<table border="1">
	<tr>
	<th>Name</th>
	<th>Sales</th>
	</tr>
	<%
	Do While Not rstSearch.EOF
		%>
		<tr>
		<td><%= rstSearch.Fields("first_name").Value %> <%= rstSearch.Fields("last_name").Value %></td>
		<td><%= rstSearch.Fields("sales").Value %></td>
		</tr>
		<%
		rstSearch.MoveNext
	Loop
	%>
	</table>
	<%
	' Close our recordset and connection and dispose of the objects
	rstSearch.Close
	Set rstSearch = Nothing
	cnnSearch.Close
	Set cnnSearch = Nothing
End If
%>

Back the the Sample Output

Back to the Sample Index


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers