20 – web applications: writing data to databases using asp

20
Mark Dixon Page 1 20 – Web applications: Writing data to Databases using ASP

Upload: everly

Post on 07-Jan-2016

23 views

Category:

Documents


6 download

DESCRIPTION

20 – Web applications: Writing data to Databases using ASP. Questions: HTML in VB. Are these correct (assume variables and fields exist)? f = f + rs.Fields(" Description ").value h = h + rs.Fields(" Name ").value a = "" + a "" html = html + - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 1

20 – Web applications:Writing data to Databases

using ASP

Page 2: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 2

Questions: HTML in VB• Are these correct (assume variables and

fields exist)?

f = f + rs.Fields("Description").value

h = h + rs.Fields("<br />Name").value

a = "<p>" + a "</p>"

html = html + <img src=face.gif />

h = "<table>" + h + "</table>"

Page 3: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 3

Questions: Databases• How many primary keys?• How many foreign keys?

32

PlantPlantID EnglishName ScientificName Price Toxic FileName

1 Foxglove Digitalis purpurea 2.5 TRUE Foxglove.jpg2 Daisy Bellis perennis 0.45 FALSE Daisy.jpg3 Hemlock Conium maculatum 8.79 TRUE Hemlock.jpg4 Marsh Mallow Althaea officinalis 3.25 FALSE MarshMallow.jpg5 Lords-and-Ladies Arum maculatum 2.25 TRUE Lords.jpg6 Wild Carrot Daucus carota 1.25 FALSE WildCarrot.jpg7 Bluebell Hyacinthoides non-scripta 1.8 FALSE Bluebell.jpg8 Common Poppy Papaver rhoeas 1.28 FALSE Poppy.jpg

OrderOrderID CustID PlantID Quantity Date Current

1 1 7 10 14-Mar-06 TRUE2 2 5 2 14-Mar-06 TRUE3 1 3 1 14-Mar-06 FALSE5 2 4 4 14-Mar-06 FALSE

46 1 2 9 09-Jun-06 FALSE

CustomerCustID Surname Forenames email Password

1 Dixon Mark [email protected] a2 Jones Sally [email protected] sally

Page 4: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 4

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

Page 5: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 5

Searching for Data• Recordset methods

– Find: searches for the next record to match given criteria string:

• e.g. "Name = 'Smith' "( " are for VB string)( ' are for database string)

Page 6: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 6

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:– record set does not persist between pages– Difficult to use .MoveNext and .MovePrev

recordset methods:

Page 7: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 7

Example: Person v1Sub Page_Load()Const cs = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb;"Dim rs As ObjectDim s As StringDim id As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs, 3)

id = CStr(Session("curID")) If id > "" Then rs.Find("[ID] = " + id) If Request.Form("btnPrev") > "" Then rs.MovePrevious() ElseIf Request.Form("btnNext") > "" Then rs.MoveNext() End If End If

Session("curID") = rs.Fields("ID").Value s = rs.Fields("Surname").Value + "<br>" parData.InnerHtml = s

rs.Close() rs = NothingEnd Sub

Use session variable torecord current position in db

Find last position in db(using id from session variable)

Page 8: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 8

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

Page 9: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 9

Database Permissions 1• Windows Explorer

– Tools– Folder Options– View Tab

• Need to turn'simple file sharing' off(as this disables the security tab in file properties)

Page 10: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 10

Database Permissions 2• 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 XP)

Page 11: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 11

Database Permissions 3• Click Security tab

• Click Add button

Page 12: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 12

Database Permissions 4• Click Advanced

button

Page 13: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 13

• Select Internet Guest Account IUSR_ … ClickFind button

Clickuser

ClickOK button

Database Permissions 5

Page 14: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 14

Database Permissions 6• Select Internet

Guest Account

• Ensure writeaccess is on

• Repeat forASPNET account

Page 15: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 15

Changing Data• Recordset methods

– AddNew: inserts a new record and makes it current

– rd.Fields("FieldName").value = "Data"

– Update: sends changes back to DB

– Delete: deletes currently selected record

Page 16: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 16

Writing data to a database• create recordset• open recordset

– dynamic cursor (3), pessimistic locking (3)

• to add a record– use to AddNew methodrs.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"

Page 17: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 17

Example: Person v2Sub Page_Load()Const cs = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\People.mdb;"Dim rs As ObjectDim id As String rs = CreateObject("ADODB.Recordset") rs.Open("Person", cs, 3, 3)

id = CStr(Session("curID")) If id > "" Then rs.Find("[ID] = " + id) If Request.Form("btnPrev") > "" Then rs.MovePrevious() ElseIf Request.Form("btnNext") > "" Then rs.MoveNext() ElseIf Request.Form("btnSave") > "" Then rs.Fields("Surname").Value = txtSurname.Value rs.Update() End If End If

Session("curID") = rs.Fields("ID").Value txtSurname.Value = rs.Fields("Surname").Value

rs.Close() rs = NothingEnd Sub

Page 18: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 18

Example: Person v3

Page 19: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 19

Tutorial Exercise: Person v1• LEARNING OBJECTIVE:

use session variable to store idpractice problem solving skills

• Task 1: Get the Person (v1) example from the lecture working.

• Task 2: Modify your code, so that all fields are displayed (use a table).

• 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. Hint: You can't stop it 'falling off' the end of the recordset. But you can detect it using EOF and move back.

Page 20: 20 – Web applications: Writing data to Databases using ASP

Mark Dixon Page 20

Tutorial Exercise: Person v2 & v3• 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.• Task 3: Modify your code, so that an add button is

included, which allows a new record to be added.• Task 4: Modify your code, so that a delete button is

included, which allows the current record to be deleted.