Using Recordset Data - Page 5
November 27, 2000
We'll create a page that opens a recordset based on the
items table of the Clothier database
(see Appendix B for the source and structure of this database).
From that recordset, we'll perform four tasks:
- Writing the name of the first item on the page
- Putting the type of the first item into a variable and then print that
variable to the page
- Using an If..Then structure so that items with less than 10 to a box
show the warning "Small Box"
- Printing the price of the first item (using the Format function to
make it with two decimal places only)
The following listing shows the code to generate the required page:
<%
dim oRSi
set oRSi=server.CreateObject("adodb.recordset")
oRSi.open "items", "DSN=clothier"
oRSi.MoveFirst
Response.Write "Next line is a simple write of data:<BR>"
Response.Write oRSi("ItemName") & "<BR><BR>"
Response.Write
"Next line is writing a variable that holds the data:<BR>"
dim varItemName
varItemName = oRSi("ItemType")
Response.Write varItemName & "<BR><BR>"
Response.Write "Next line is deciding what to write based on an
If...Then using the data:<BR>"
If oRSi("ItemQtyPerBox")<10 then
Response.Write "Small Box<BR><BR>"
Else
Response.Write "Large Box<BR><BR>"
End If
Response.Write
"Next line uses the data as an argument for a function:<BR>"
Response.Write UCase(oRSi("ItemDepartment")) & "<BR><BR>"
%>
[Note: The 10th and 11th lines above are one line as are the
16th and 17th as are the 24th and 25th. They were split for
formatting purposes]
Your page should appear as below in your browser:
|
How It Works - Using Recordset Data
The first few lines in the next listing create the recordset. A
recordset will open with the pointer at record one, so in this
simple case there is actually no need for the MoveFirst method.
On the other hand, if a recordset has been opened earlier and
been in use, you would want to use the MoveFirst so you are sure
you are at BOF.
|
<%
dim oRSi
set oRSi=server.CreateObject("adodb.recordset")
oRSi.open "items", "DSN=clothier"
oRSi.MoveFirst
In our first section we merely need to print the data which is
returned from the recordset, as follows:
Response.Write "Next line is a simple write of data:<BR>"
Response.Write oRSi("ItemName") & "<BR><BR>"
But if we can also put data into a variable which can be used
later, in the following case to write to the page:
Response.Write
"Next line is writing a variable that holds the data:<BR>"
dim varItemName
varItemName = oRSi("ItemType")
Response.Write varItemName & "<BR><BR>"
[Note: The 1st and 2nd lines above are one line. They were
split for formatting purposes]
As we can see in the next list, you can use data without printing
it to the page. Below we use it in a test to determine which of
two messages to write to the page:
Response.Write "Next line is deciding what to write based on an
If...Then using the data:<BR>"
If oRSi("ItemQtyPerBox")<10 then
Response.Write "Small Box<BR><BR>"
Else
Response.Write "Large Box<BR><BR>"
End If
[Note: The 1st and 2nd lines above are one line. They were
split for formatting purposes]
Another option is to use the data returned from the recordset as
an argument in a function. Below we use the name of the
department of the item as the argument for the Upper Case
function.
Response.Write
"Next line uses the data as an argument for a function:<BR>"
Response.Write ucase(oRSi("ItemDepartment")) & "<BR><BR>"
%>
[Note: The 1st and 2nd lines above are one line. They were
split for formatting purposes]
Using Data - Page 4
Beginning ASP Databases
Which Record? - Page 6
|