Data transfer happens in one of two directions: either retrieving it from the database to display in an HMTL page, or sending it to the database.
Retrieving
[]
By far, the easiest. The code in the previous lecture already shows you how to open a RecordSet. All you need to do now is get the data. This is done with the Fields collection, and works in the same manner as the Form or QueryString collections of the Request object. You may do it either inside a code block by assigning the value stored in the field to a variable or as in-line code. The following examples demonstrate both methods:
[][]
<%
strFirstName = objRecordSet.Fields("FirstName")
%>
<%= objRecordSet.Fields("FirstName") %>
[
][
]
The complicated part is formatting the data to appear nicely in the HTML. For a list, the trick is creating the HTML table rows inside an ASP While loop. For detail, use the in-line style to insert the values in the appropriate table cell. It takes a mix of ASP and HTML to produce the effect. The following two links will display the source code for the Addresses example from Module 3:
[]
View List
View Detail
Edit Detail
[
]
Note in the examples that I use code to check that the records were returned or the database was updated. To check that records were returned, I use the following line:
[][]
<% If Not rstAddresses.EOF and Not rstAddresses.BOF and rstAddresses.RecordCount<>0 %>
[
][
]
I'm using three functions of the RecordSet Object to test that I actually recevied records. Look up theses functions in the text and determine what it is they do. How does this compound If statement ensure that I have records?
[
]
Sending
[]
Sending data is a bit more complicated. It involves executing an appropriate INSERT, UPDATE, or DELETE query. The query needs to be created as a String, then use the Execute method on the connection:
[][]
conAddresses.Execute(strSQL), rowsAffected, 1
[
][
]
rowsAffected is a variable that will store the number of record affected by the update. If I am adding a record, changing a record, or deleting a record, I would expect this value to be 1. If I delete 10, records, the value would be 10 if all records were successfully deleted. You know how many records should be affected. Always compare this variable against that value to make sure the update succeeded.
You will find the meaning of the third argument in Chapter 7 of the ASP text.
Take a close look at Update Detail.
[]
For an INSERT query, the format for the SQL is:
[]
INSERT INTO Table(Comma-separated field list) VALUES(Comm-separate value list)
The For loop in the code creates the two lists as variable strings. Once the loop is complete, the string variables are concatenated with the rest of the SQL.
Note that the AddressID field is ignored, since this is an auto-number field.
[
]
For an UPDATE query, the format for the SQL is:
[]
UPDATE Table SET
The For loop creates the list as a variable string. Once the loop is complete, the string variable is concatenated with the rest of the SQL.
Note that the AddressID field is used in the WHERE clause. This ensures that just the one record is updated.
[
]
For a DELETE query, the format for the SQL is:
[]
DELETE FROM Table
Note that the AddressID field is used in the WHERE clause. This ensures that just the one record is deleted.
[
]
One last issue is file permission.
[]
Under Win98, this will not be a problem. You will be allowed to run queries that change the database.
Under Win2K, you will need to set security permission to allow changes to the database.
Locate the database with Windows Explorer.
Right-click on the file, and choose "Properties" from the bottom of the pop-up menu.
Click on the "Security" Tab.
Click on the "Add" button.
Locate and select the user that begins with "IUSER_"
Click on the "OK" button.
Checkmark all boxes in the "Allow" column.
Click on the "Apply" button.
Click on the "OK" button.
[
]
[]
[
]