ASP Instructional exercise HTML Structure to Database Availability - PowerPoint PPT Presentation

asp tutorial html form to database connectivity l.
Skip this Video
Loading SlideShow in 5 Seconds..
ASP Instructional exercise HTML Structure to Database Availability PowerPoint Presentation
ASP Instructional exercise HTML Structure to Database Availability

play fullscreen
1 / 18
Download Presentation

ASP Instructional exercise HTML Structure to Database Availability

Presentation Transcript

  1. ASP Tutorial HTML Form to Database Connectivity D. Nord

  2. Database Connectivity(1 of 3) • You can connect to the database in two ways. (You can access the following info to discuss the two options. -One more • DSN connection (DSN stands for Data Source Name – this method can be used if you already have a server set up for the web pages via ODBC) • DSN-less connection (this is an easier way to connect to your database – you do not have to configure your computer to set this up) • The following code shows you how to connect to your access database with a DSN-less connection. D. Nord

  3. Create a Database(2 of 3) • First create a database named “comments.mdb” using Microsoft Access. • Then create a table and name it “commentadd”. • Include the following fields in the table: • name • comments • email D. Nord

  4. Database Table Before data is entered(3 of 3) D. Nord

  5. Connecting to a Database(1 of 5) • Next, create a form with the fields you require. • Name the file anything you want. Ex: contact.htm • In the <form method="POST" action="addrecord.asp"> area in the form, change the action=“addrecord.asp” to whatever name you want and include the .asp extension. D. Nord

  6. Creating the HTML Form(2 of 5) • <html> • <head> • <title>Feedback Form</title> • </head> • <body> • <p><b><font size="6" face="Century Gothic">Feedback Form</font></b></p> • <p><font size="4" face="Century Gothic">Please fill out this form to help us improve our site.</font></p> D. Nord

  7. (3 of 5) • <form method="POST" action="addrecord2.asp"> • <p><b>Name: </b> • <input name="name" type="text" size="32"></p> • <p><b>Comments: </b> • <textarea rows="4" name="comments" cols="30"></textarea></p> • <p><b>Email Address: </b> • <input type="text" name="email" size="26"></font></p> D. Nord

  8. (4 of 5) • <p>&nbsp;</p> • <p> • <input type="submit" value="Submit Your Entries" name="B1"><input type="reset" value="Clear Your Entries" name="B2"> • </p> • </form> • </body> • </html> D. Nord

  9. Form Page - Example(5 of 5) D. Nord

  10. Connecting to a Database(1 of 8) • Next create the ASP response pageand give it the same name as is found in the form in “action=…..” • The following slides contain the ASP code to connect to the database. • Lines 3 – 6 declare all the variables required. • Line 10 - sets up the connection to the database and opens the connection with the database D. Nord

  11. Code Explanation(2 of 8) • Line 19 opens the table in the database using the method. • Line 20 - 24 adds new data from the form tothe table using the rs.addnew method. • Line 25 - 27 updates the table and moves to end of the record. • Lines 28 - 30 closes all the connections to the database. D. Nord

  12. Connecting to a Database(3 of 8) • <%@ Language="VBScript"%> • <% • 'Declare all local variables • dim rs • dim strconn • dim strID D. Nord

  13. (4 of 8) 7. ‘Sets up the connection to the database(The entire line 9 must be on one line) • set strconn=server.createobject("adodb.connection") • strConn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("database.mdb") • 'Create an ADO recordset object • set rs = server.createobject("adodb.recordset") • 'Initialise the strID variable with an SQL statement to query the database • strID = "SELECT commentadd.Name, commentadd.comments, FROM commentadd;“ • 'Set the cursor type we are using so we can navigate through the recordset • rs.CursorType = 2 • 'Set the lock type so that the record is locked by ADO when it is updated • rs.lockType = 3 • 'Open the recordset with the SQL query(the 2,2 deals with writing and locking the table) • rs.Open strID, strconn, 2,2 D. Nord

  14. (5 of 8) 20. ‘Adds new data from the Form to the Table 21. rs.addnew 22. rs("name") = request("name") 23. rs("comments") = request("comments") 24. rs("email") = request("email") 25. ‘Updates the Table and Moves to end of record 26. rs.update 27. rs.movelast 28. ‘Closes all the connections to the Database 29. set rs = nothing 30. set conn = nothing 31. %> D. Nord

  15. (6 of 8) 32. <html> 33. <head> 34. <title>Thank you</title> 35. </head> 36. <body> 37. <center>Thank you</center><br> 38. <center>Please click <a href="/index.html">here</a> 39. to return to the home page.</center> 40. </body> 41. </html> D. Nord

  16. ASP Response Page(7 of 8) This is the response page that is displayed once the form is submitted. This page is generated by the ASP code shown in the previous slides. The message displayed above can be changed to show your own message. D. Nord

  17. Access Database(8 of 8) This is what the database will look like after the form is submitted. The data from the form is saved in the database. D. Nord

  18. Components and Objects(These are different than the ADO Objects) • ASP consists of five primarybuilt in objects • Application – manages Web application's information • Request – retrieves information from a browser for processing at the server • Response – transmits information from a Web server to browser • Server – controls behavior of a Web server • Session – tracks and manages individual user sessions within particular Web application The built-in objects are special because they are built into ASP and need not to be declared or created before they can be used in scripts. D. Nord