Database Code
  Home arrow Database Code arrow Adhoc ASP Search page
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE CODE

Adhoc ASP Search page
By: aspfree
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    1999-09-01

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    ASPSearch!
    Purple= HTML Code
    Black= HTML Text
    Green= Server-side ASP code
    Red= My Comments

    <%@ LANGUAGE="VBSCRIPT" %>
    <html>
    <head>
    <title>Da Search Results!</title>
    </head>
    <body>
    Here is the simple stuff!  Line one tells the IIS server to expect some VBScript ahead and the rest of it just builds the HTML file that will be displayed.
    <%
    Dim SqlJunk

    Set dbGlobalWeb = Server.CreateObject("ADODB.Connection")
    dbGlobalWeb.Open("Employees")

    SqlJunk = "SELECT * FROM Employees"

    If Request.Form("TypeSearch") = "FirstName" Then
    SqlJunk = SqlJunk & " WHERE FirstName LIKE '%"        &_
    Request.Form("DaInBox") & "%'"
    End If

    If Request.Form("TypeSearch") = "LastName" Then
    SqlJunk = SqlJunk & " WHERE LastName LIKE '%"        & _
    Request.Form("DaInBox") & "%'"
    End If

    If Request.Form("TypeSearch") = "Title" Then
    SqlJunk = SqlJunk & " WHERE Title LIKE '%"            &
    _
    Request.Form("DaInBox") & "%'"
    End If

    If Request.Form("TypeSearch") = "Division" Then
    SqlJunk = SqlJunk & " WHERE Division LIKE '%"          & _
    Request.Form("DaInBox") & "%'"
    End If

    If Request.Form("TypeSearch") = "Phone" Then
    SqlJunk = SqlJunk & " WHERE Phone LIKE '%"            & _
    Request.Form("DaInBox") & "%'"
    End If

    If Request.Form("TypeSearch") = "Email" Then
    SqlJunk = SqlJunk & " WHERE EMail LIKE '%"            &_
    Request.Form("DaInBox") & "%'"
    End If

     

    We start off the ASP code by defining a variable called "SqlJunk" which I will use later on to build a string which I will plug into a SQL statement that defines the Recordset.

    The next paragraph "Set dbGlobalWeb...." creates an ADODB connection.  After I create an object, I have to open the database that I will be using.  I tell it to open a connection I have called Employees.

    Here is where you have to do a little server administration: Make a DSN through the ODBC icon in the Control Panel called "Employees."   If you're saying, "huh?" right now, just follow directions and you'll understand as you go along.  The DSN tab is the second tab from the left and you will need to "Add" a new DSN to an Access database. Find the Access MDB file I called "Employees" and point your "Employees" DSN to the "employees.mdb" file I have supplied for you. (By the way, you could have named your DSN "KrustyTheClown" and it would work just fine as long as you change the "dbGlobalWeb.Open("Employees") to dbGlobalWeb.Open("KrustyTheClown"))

    The next paragraph fills the variable("SqlJunk") with the first part of your SQL statement.  The statement says, "SELECT every field in the "Employees" table.  (When I reread this I realized that many people might be getting confused here because I named my DSN "Employees", my database "Employees" and the table within the database "Employees."  Force yourself to look at the source code closely to figure out which "Employees" I'm talking about.) I could claim that I intentionally did this to force you to learn but for some reason I have a tendency to code subsets of things using the same names.  It works for me!

    The next six "If...Then" statements could have been written as a "Select...Case"- oh well. Initially I only wrote it for two fields and then someone said, "you should make it so they can search all the the fields so I just cut and pasted the rest.  Hey, it works, alright?! (The   &_  just continues the code on a new line like VB)

    Anyway, the first line of every If statement Requests the Form field I call "TypeSearch" and asks if it's equal to a value- "FirstName", "LastName","Title","Division", etc.

    The second line of every "If" statement says, "If the above line was true, Then add the rest of the appropriate SQL string code onto the end of the existing "SqlJunk" string." The second half of my SQL statement that I'm sticking into the "SqlJunk" variable completes the SQL statement that I will use to find the records that match your search criteria. The SQL statement now says, "SELECT/Find every record in the Employees table WHERE the "such&such" field has data that is similar/LIKE the input data the user has put in the box called "DaInBox." You'll notice that the Request.Form("DaInBox") is surrounded by &,%,",'.   Ok here's what they're for:  The % is used by SQL as wildcards like DOS used the *.  The & is used to paste the two separate strings to make one long string in the SqlJunk variable.  The ' holds the wildcards and the " holds the statement.

    Each "If" statement concludes with an "End If."

    Set rsGlobalWeb = Server.CreateObject("ADODB.Recordset")
    rsGlobalWeb.Open SqlJunk, dbGlobalWeb, 3,3
    %>
    Here is where I create the ADODB.Recordset!  It seems like I've done a lot at this point but really all I've done is made a connection to a database and stick pieces of an SQL statement into a variable I call SqlJunk.  In the process of defining what my recordset is going to be, I'm also going create and name my recordset variable(rsGlobalWeb)  that I will use to extract the field data within my recordset.

    The second line on this page defines the attributes of your ADODB.Recordset object that I have named rsGlobalWeb. This line says, "Open the rsGlobalWeb recordset with the following SQL statement contained within the SqlJunk variable using the connection object we established earlier called dbGobalWeb."  The 3 stuff just opens the database with "optimistic" options- meaning it allows multiple connections and assumes no cruddy people are going to try to hack your data.

    <% If rsGlobalWeb.BOF and rsGlobalWeb.EOF Then %>
    <h2 align="center">We did not find a match!</h2>
    <%Else%>
    What this code says is, "If the recordset(rsGlobalWeb) is at the beginning(BOF) and end(EOF) of the recordset at the same time Then logically there is no recordset and logically there is no match!  If there is something Else other than nothing then go to the next line.
    <%If Not rsGlobalWeb.BOF Then%>

    <h2>Here are the results of your search:</h2>

    <table BORDER="0" width="100%" cellpadding="3">

    <tr>

    <th bgcolor="#800000"><font face="Arial" color="#FFFFFF">Name </font></th>
    <th bgcolor="#800000"><font face="Arial" color="#FFFFFF">Title </font></th>
    <th bgcolor="#800000"><font face="Arial" color="#FFFFFF">Division </font></th>
    <th bgcolor="#800000"><font face="Arial" color="#FFFFFF">Phone </font></th>
    <th bgcolor="#800000"><font face="Arial" color="#FFFFFF">E-Mail </font></th>
    </tr>
    <% Do While Not rsGlobalWeb.EOF %>

    <tr>

    <td><%=rsGlobalWeb("FirstName")%>&#32 <%=rsGlobalWeb("LastName")%>

    </td>

    <td><%=rsGlobalWeb("Title")%>

    </td>

    <td><%=rsGlobalWeb("Division")%>

    </td>

    <td><%=rsGlobalWeb("Phone")%>

    </td>

    <td><a href="mailto:<%=rsGlobalWeb("Email")%>"><%=rsGlobalWeb("Email")%></a> </td>

    </tr>

    <%
    rsGlobalWeb.MoveNext
    Loop
    %>

    </table>

    <%End If%>

    <%End If%>

    The first line here basically says, "If the recordset is not at the BOF or "Beginning Of File" Then execute this HTML code which defines the header information for the query results."

    We make a pretty table with field headers that corresponds to the fields in the database.

    <%Do While Not....%>Now we tell it to execute the following code as long as we are not at the end of the recordset(EOF). Now the ASP code starts sticking records underneath the field headers and extracts the field data from the recordset until it reaches the end of the records it has found.  I extract the field data within the database using <%=rsGlobalWeb("FieldName")%>

    The Loop statement just tells ASP to keep going back up to the Do While statement until it is finished.

     

    Finally, remember to End your If's.

    <%
    rsGlobalWeb.Close
    dbGlobalWeb.Close
    %>

    </body>
    </html>
    I close the recordset and the connection to the database and I finish the HTML coding for the results page.

    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Database Code Articles
    More By aspfree

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Download a free trial of WebSphere Business Modeler Advanced V6.1.1

    Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement.
    FREE! Go There Now!


    NEW! Download IBM Rational Developer for System z

    Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects.
    FREE! Go There Now!


    NEW! Hacking 101

    Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today.
    FREE! Go There Now!


    NEW! Hello World: Monitor a simple business process using WebSphere Business Monitor V6.0.2

    This tutorial shows new users of IBM WebSphere Business Monitor Version 6.0.2 how to perform the "Hello World" equivalent for monitoring business process applications. It is intended to help you get familiar with the capabilities of the product.
    FREE! Go There Now!


    NEW! Rational Talks to You: Manage RUP-based CMMI initiatives

    Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered!
    FREE! Go There Now!


    NEW! The dirty dozen: preventing common application-level hack attacks

    As organizations have grown increasingly dependent on online software, the risk of malicious attacks has also become far more serious. Fortunately, well-governed organizations can protect their Web applications by injecting vulnerability assessments and ethical hacks into their software development and delivery processes. This paper describes 12 of the most common hacker attacks and provides basic rules that you can follow to help create more hack-resistant Web applications.
    FREE! Go There Now!


    NEW! The role of integrated requirements management in software delivery

    This paper is about the critical role that a discipline called integrated require­ments management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrat­ing, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way.
    FREE! Go There Now!


    NEW! Understanding Web application security challenges

    As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security.
    FREE! Go There Now!


    NEW! Webcast: Extreme transaction processing with WebSphere Extended Deployment

    In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications.
    FREE! Go There Now!


    Refresh! IBM Rational Systems Development Solution eKit

    With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    DATABASE CODE ARTICLES

    - Deployment of the MobiLink Synchronization M...
    - MobiLink Synchronization Wizard in SQL Anywh...
    - Finding Matching Records in Data Access Pages
    - Using the AccessDataSource Control in VS 2005
    - A Closer Look at ADO.NET: The Command Object
    - A Closer Look at ADO.NET: The Connection Obj...
    - Using ADO to Communicate with the Database, ...
    - Code Snippets: Counting Records
    - Constraints In Microsoft SQL Server 2000
    - Multilingual entries into a DB and to be dis...
    - Getting A List of Tables From SQL Server
    - SQL Server Database Creator - .NET Version
    - ADO Recordset Paging
    - Two combos, one textbox example
    - Discussion & Listserv Module by Mike Eck...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT