First Name
Last Name
Designation
36 Module 8: Accessing a Database The Request object of ASPs is used to retrieve the form inputs and the data is stored in a database The Connection object and the Recordset object are used to display data from the database The code for Connect.asp is as follows: USING ADO Accessing Data From a Database First Name Last Name Designation Module 8: Accessing a Database Navigating Records Slide Objective ! Lead-in MoveFirst ! MoveNext ! To describe the methods used to navigate records MovePrevious ! MoveLast Sub Previous_onClick Sub Previous_onClick rs.movePrevious rs.movePrevious If rs.bof then If rs.bof then rs.movefirst rs.movefirst End if End if End Sub End Sub ADO provides methods to navigate through the records in a recordset Sub Next_onClick Sub Next_onClick rs.MoveNext rs.MoveNext If rs.eof then If rs.eof then rs.MoveLast rs.MoveLast End If End If End Sub End Sub Delivery Tip Open ADO.asp from \Inetpub\wwwroot\1912\Sam papps and explain the use of loops and EOF properties with the Recordset object Sub Sub until rs.EOF until rs.EOF response.write "" response.write "" response.write "" & _ response.write "" & _ rs("FName") & "" rs("FName") & "" response.write "" & _ response.write "" & _ rs("LName") & "" rs("LName") & "" response.write "" response.write "" rs.MoveNext rs.MoveNext loop loop (View ADO.asp from Sampapps) The results of a query are stored as records in rows There are methods that allow you to navigate through the records in a recordset You can create buttons in a document that enable users to navigate through the records of a recordset This allows you to display only one record at a time There are four methods that you can use to move from one record to another They are: ! MoveFirst ! MovePrevious ! MoveNext ! MoveLast The following code points to the first record in the recordset: Sub Previous_onClick rs.MoveFirst End Sub When using the MovePrevious and MoveNext methods, you must be careful not to move past the end of the recordset The BOF and EOF properties of the recordset object check for the first and last record The BOF property is set to True if the record pointer is positioned before the first record in the recordset Similarly, the EOF property is set to True when the record pointer is positioned after the last record 37 38 Module 8: Accessing a Database The following code uses the MovePrevious and the MoveLast property to navigate through the records in the recordset The code also shows the use of the BOF and EOF properties: Sub window_onLoad Set rs = MyDSO.Recordset End Sub Sub Previous_onClick rs.movePrevious If rs.bof then rs.movefirst End if End Sub Sub Next_onClick rs.MoveNext If rs.eof then rs.MoveLAst End If End Sub The following example uses the Do…Until loop to move through all records in … a recordset until the value of EOF is True: until rs.EOF response.write response.write response.write response.write rs.MoveNext loop "" "" & rs("FName") & "" "" & rs("LName") & "" "" Module 8: Accessing a Database 39 Lab 8: Retrieving Data from a Database Using ASP Slide Objective To introduce the lab Lead-in In this lab, you will retrieve data from a database using ASP Explain the lab objectives To retrieve data from a database using ASP Objectives After completing this lab, you will be able to: ! Add data to a database by using ASP ! Access data from a database by using the ADO Connection object Prerequisites Before working on this lab, you must have: ! Created a Web page using Microsoft FrontPage 2000 ! Retrieved form input by using ASP Scenario Contoso Ltd wants to create a guest book where visitors can enter the company details Visitors need to enter the customer id, company name, name of the person to contact, contact title, address, city, and country The information of the visitors is stored in a database Also, visitors can view the guest book database for information about a particular company Estimated time to complete this lab: 60 minutes 40 Module 8: Accessing a Database Exercise Creating a Guest Form In this exercise, you will create a form in FrontPage 2000 ! Create a form in FrontPage 2000 Create a new one-page Web under http://localhost/1912/Mywebpages/Lab08web On the page, add a form with seven text boxes and a Submit button Label these text boxes as Customer ID, Company Name, Contact Name, Contact Title, Address, City, and Country Specify the name for the text boxes as txtid, txtname, txtcontact, txttitle, txtaddress, txtcity, and txtcountry respectively Set the form's Action property to database.asp (You will create database.asp in a later exercise) Save the page as Labform.htm Module 8: Accessing a Database Exercise Create a Data Source Name In this exercise, you will create a data source name ! Create a data source name • Create a data source name with the following specifications: Name: MyNewDatabase Driver: Microsoft Access Driver Database: Contoso.mdb You will find Contoso.mdb under /Labs/Lab08/Starter 41 42 Module 8: Accessing a Database Exercise Accessing data from a database In this exercise, you will create an ASP file to access data from a database ! Access data from a database Open a new page Use the REQUEST object to retrieve form input Open a connection with the data source MyNewDatabase by using the CONNECTION object of ADO Create a recordset object to retrieve from and insert data into the database Create a SQL query to insert visitor data into the database Create a table to retrieve visitor data from the database To see an example of what your ASP should look like, see Appendix A or the Student CD-ROM To view the instructor sample code, seeAppendix A, Lab 8, Exercise Save the file as database.asp Module 8: Accessing a Database Exercise Viewing the file In this exercise, you will view the file ! View the file Open the file Labform.htm in Internet Explorer 5.0 Enter information about a customer, submit the information, and view the results 43 44 Module 8: Accessing a Database Review Slide Objective To reinforce module objectives by reviewing key points Lead-in The review questions cover some of the key concepts taught in the module ! Access data from a text file using Tabular Data Control (TDC) ! Navigate through records by using the methods of TDC ! Access data from an XML document ! Describe the data access interfaces ! Create a data source name ! Access a database by using the Database Results Wizard in FrontPage 2000 ! Connect to a data source by using Active Data Objects (ADO) ! Process input form information by using Active Server Pages (ASPs) How you instantiate a TDC control? To instantiate a TDC control you use the tag The clsid attribute assigns a unique id to the TDC control and the ID attribute is the name with which you can refer to the TDC control later Which attribute of a TDC control allows you to specify the number of records to be displayed at one time? The datapagesize attribute of the TDC control allows you to limit the number of records displayed at one time What is an XML data island? An XML data island is XML data embedded in an HTML page It allows you to use an XML document without having to load it by means of a script or the tag Which object of ADO is used to establish a connection with a data source? The Connection object of ADO is used to establish a connection with a data source Module 8: Accessing a Database Which method of ADO is used to move the first record in a recordset? The MoveFirst method of ADO is used to move to the first record in a recordset 45 THIS PAGE INTENTIONALLY LEFT BLANK ... ODBC Database Simple Data Provider OLE DB Provider Non Mainframe Relational Data Data Database A database is a collection of data stored in a tabular form A database stores related data on a particular... /Labs/Lab08/Starter 41 42 Module 8: Accessing a Database Exercise Accessing data from a database In this exercise, you will create an ASP file to access data from a database ! Access... to a standard database interface The standard database interfaces are ODBC and OLE DB ODBC ODBC is designed to allow access to relational database systems, such as Access and Oracle Most database