Getting Data from the database into Flash
ASP gets the data from the database and sends it to your Flash interface
In this step of our Flash and ASP integration guide, ASP interacts with the database and sends the required data for display to the Flash display form. This section is divided into two parts:
- The ASP page interacts with the Database and gets the required data
- ASP sends the retrieved data to Flash
Note: Values or code you will have to enter are given in light blue. All ASP code can be directly cut and pasted into your file. Comments in ASP are in green.
ASP Gets the Data from the Database
- Open your GuestBookGetCode.asp
file. You will have to do the following steps to get data from
the database.
- Connect to the Database
- Select the required data and get it into a recordset with a page size (the maximum no. of records you would like to load at a time).
- Below is shown the code to accomplish these steps. Add it to the existing code in the file. You can cut and paste the code but it is good practice to understand what it does first.
Connecting to the Database and getting data from it through ASP
<% '---------------This file is the GuestBookGetCode.asp file-------------------------
'Start Declaring all variable used by you (you can do this as you blue)
Dim DBConn, strDB, rsGuestBook, x, intSize, strSelectSQL, strDate, fldName, fldEmail, fldMess, fldDate, intPage, intStart, intFinish, intCount, intPageCount, intRecord, intNav,blnSubmit
'Give the Database Connection String
strDB = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("dbGuestbook.mdb") & ";DefaultDir=" & Server.MapPath(".") & ";DriverId=25;FIL=MS Access;MaxBufferSize=512;PageTimeout=5"
'Get today's Date and give the number of entries you would like to load per batch
strDate=CDate(Date)
intSize=50
'Get the variable to determine if a submission has been made
blnSubmit=request("submit")
'Get the Batch Number of the Guest-Book entries
intNav=request.QueryString("NAV")
if intNav = "" then intPage = 1 else intPage = cint(intNav)
'Give the SQL Select Statement
strSelectSQL="SELECT fldName, fldEmail, fldMessage, fldDate FROM tblGuestBook ORDER BY fldDate DESC , fldGID DESC"
'Create the Recordset with Read Only - Static (Forward and Backward Cursor) properties
Set DBConn = Server.CreateObject("ADODB.Connection")
DBConn.Open strDB
set rsGuestBook=Server.CreateObject("ADODB.Recordset")
rsGuestBook.ActiveConnection = DBConn
rsGuestBook.Source = strSelectSQL
rsGuestBook.CursorType = 3
rsGuestBook.CursorLocation = 3
rsGuestBook.LockType = 1
rsGuestBook.Open
'The recordset is paged for optimization of the code
rsGuestBook.PageSize = intSize
rsGuestBook.CacheSize = intSize
intPageCount = rsGuestBook.PageCount
intCount = rsGuestBook.RecordCount
Set rsGuestBook.ActiveConnection = Nothing
If (not rsGuestBook.EOF) then
rsGuestBook.AbsolutePage = intPage
intStart = rsGuestBook.AbsolutePosition
End if
if CInt(intPage) = CInt(intPageCount) then
intFinish = intCount
else
intFinish = intStart + (rsGuestBook.PageSize - 1)
end if
%>
ASP Send the Data to Flash
- Now the ASP file is ready to send the Data to Flash. Remember this is done in the Variable-Value format. Add the following code to the same GuestBookGetCode.asp file as a continuation of the above.
Supplying Data to the Flash Interface by the ASP file
<% '---------------Output the Data in the 'Variable-Value' format-----------------
response.write "&size=" & intSize & "&count=" & intCount "
if intCount=0 then response.write "&f_name=No Guest Entries Yet"
'The Flash interface needs an Initial Value for displaying the first record
if not rsGuestBook.Eof then
if blnSubmit="True" then rsGuestBook.Movenext
if not rsGuestBook.Eof then
set fldDate=rsGuestBook("fldDate")
set fldName=rsGuestBook("fldName")
set fldEmail=rsGuestBook("fldEmail")
set fldMess=rsGuestBook("fldMessage")
response.write "&startN=" & fldName & "&startE=" & fldEmail & "&startM=" & fldMess & "&startD=" & fldDate & "&date=" & strDate
response.flush
end if
end if
'Now output all the records, each with an identifying index, value 'x'
x = ((intPage-1)*intSize)+1
For intRecord = 1 to rsGuestBook.PageSize
if rsGuestbook.EOF then Exit For
response.write "&name" & x & "=" & fldName & "&email" & x & "=" & fldEmail & "&mess" & x & "=" & fldMess & "&date" & x & "=" & fldDate
x = x + 1
response.flush
rsGuestBook.Movenext
Next
'Now close the recordset and database connection. Also set them and the fields (that were set to values from fields in the recordset) to Nothing
rsGuestBook.Close
DBConn.Close
Set rsGuestBook = nothing
Set fldName=nothing
Set fldEmail=nothing
Set fldMess=nothing
Set DBConn = nothing
%>
Congratulations you have finished the ASP part of sending data to Flash.
Continue to Step 5B: Flash Displays the Data
This tutorial covers the usage of ASP and Flash to bring about Flash dynamic data display and manipulation i.e. interactivity into Flash:
- Flash and ASP Introduction
- Basics of Flash and ASP
- Preparing the Stage
- Sending Data from your Flash form to the database
- Getting Data from the database into your Flash form
- Flash preloader that waits for the data to load
- Validation of fields in Flash forms using JScript and VBScript