Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: Writing data to Databases using ASP.

Similar presentations


Presentation on theme: "Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: Writing data to Databases using ASP."— Presentation transcript:

1 Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: Writing data to Databases using ASP

2 Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the fundamental ideas involved in using server-side code to write data to databases Objectives, by end of this week’s sessions, you should be able to: –create an ASP web page that allows the user to store data in database

3 Mark Dixon, SoCCE SOFT 131Page 3 Example: Person v1 (Specification) User requirement: –Display person’s details from database online, and be able to move to next and previous person (record) Problem: –Can’t use.MoveNext and.MovePrev recordset methods: record set does not persist between pages

4 Mark Dixon, SoCCE SOFT 131Page 4 Example: Person v1 (html) Person's Details Person's Details <% ' ASP code will go here (next slide). %> Self post

5 Mark Dixon, SoCCE SOFT 131Page 5 Example: Person v1 (ASP) <% Const adOpenDynamic = 3 Const cs = "…" Dim rs rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs, adOpenDynamic ) If Session("curID") <> "" Then rs. Find ( "[ID] = " & Session("curID")) If Request.Form("btnPrev") <> "" Then rs. MovePrevious() ElseIf Request.Form("btnNext") <> "" Then rs. MoveNext() End If Session("curID") = rs.Fields("ID").Value Response.Write( rs.Fields("Surname").Value & " ") Response.Write( rs.Fields("Forenames").Value) rs.Close() rs = Nothing %> Button value empty unless button was pressed Use session variable to record current position in db

6 Mark Dixon, SoCCE SOFT 131Page 6 Example: Person v2 (Specification) User requirement: Display person’s details from database online, and be able to move to next and previous person (record) –Change surname and save to database

7 Mark Dixon, SoCCE SOFT 131Page 7 Database Permissions 1 In order for ASP to write to a database –Need to give write access to Internet Guest Account for database file (People.mdb) Right-click on file in Windows Explorer (the following screens are for Windows 2000)

8 Mark Dixon, SoCCE SOFT 131Page 8 Database Permissions 2 Click Security tab Click Add button

9 Mark Dixon, SoCCE SOFT 131Page 9 Database Permissions 3 Select Internet Guest Account IUSR_ … Click Add button Click OK button

10 Mark Dixon, SoCCE SOFT 131Page 10 Database Permissions 4 Select Internet Guest Account Ensure write access is on

11 Mark Dixon, SoCCE SOFT 131Page 11 Writing data to a database create recordset open recordset –dynamic cursor (3), pessimistic locking (3) to add a record –use to AddNew method rs.AddNew to delete a record –use the Delete method rs.Delete to change existing data –assign a new value to fields rs.Fields("Surname").Value = "Fred"

12 Mark Dixon, SoCCE SOFT 131Page 12 Example: Person v2 (html) Person's Details Person's Details <% ' ASP code will go here (next slide). %> Surname: " > PersonEdit.aspx

13 Mark Dixon, SoCCE SOFT 131Page 13 Example: Person v2 (ASP) <% Const cs = "…" Dim rs Dim Surname rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs, 3, 3) If Session("curID") <> "" Then rs.Find("[ID] = " & Session("curID")) If Request.Form("btnPrev") <> "" Then rs.MovePrevious() ElseIf Request.Form("btnNext") <> "" Then rs.MoveNext() ElseIf Request.Form("btnSave") <> "" Then rs. Fields("Surname") = Request.Form("txtSurname") rs. Update() End If Session("curID") = rs.Fields("ID").Value Surname = rs.Fields("Surname").Value rs.Close() rs = Nothing %>

14 Mark Dixon, SoCCE SOFT 131Page 14 Example: Phone Book (database) Person IDSurnameForenamesPhoneNum 1DixonMark01752 232556 2JonesSally01752 111111 3SmithAlex01752 222222

15 Mark Dixon, SoCCE SOFT 131Page 15 Example: Phone Book (screen)

16 Mark Dixon, SoCCE SOFT 131Page 16 Tutorial Exercise: Person v1 Task 1: Get the Person (v1) example from the lecture working. Task 2: Modify your code, so that all fields are displayed. Task 3: Modify your code, so that it does not generate an error when the user goes past the end or beginning of the recordset.

17 Mark Dixon, SoCCE SOFT 131Page 17 Tutorial Exercise: Person v2 Task 1: Get the Person (v2) example from the lecture working. Task 2: Modify your code, so that a line of text is displayed confirming that data has been saved.

18 Mark Dixon, SoCCE SOFT 131Page 18 Tutorial Example: Phone Book Task 1: Get the Phone Book example from the lecture working. Task 2: Add a button on your page that jumps to another page, which allows a new record to be added. Task 3: Modify your code, so that each line (record) has a link leading to another page that allows the data to be edited. Task 4: Modify your code, so that each line (record) has a link leading to another page that allows the record to be deleted.


Download ppt "Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: Writing data to Databases using ASP."

Similar presentations


Ads by Google