Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Flash Database Access

0.00/5 (No votes)
1 Nov 2004 1  
How to query the database using Flash and ASP.

RSSXML image

Introduction

Flash is a hot technology to create interactive web sites. Basically, web site development falls in three prime categories:

  • Static sites
  • Dynamic sites
  • Interactive sites

The difference between a dynamic and an interactive site is the level of control that the user has over the web site. In other words, if the user is allowed to go farther than simply changing the appearance of a page (dynamic) and is able to work with it as if the page was a productivity software, then the site could be classified as interactive.

Flash enables interactive site creation with ease and convenience. Featuring a Rapid Application Development (RAD) type of Integrated Development Environment (IDE), Flash is the hottest kid on the block for interactive site development.

This article discusses how to access the database using Flash MX. Please remember that Flash is unable to natively access a database. A programming language (ASP in this case) provides the programming logic, while Flash works at the presentation tier of the application.

Using the code

There are five files included in the zip file:

  • data.asp --> programming logic
  • flashasp.fla --> Flash source file
  • flashasp.swf --> the compiled Flash movie
  • flashasp.html --> This file is used to execute the application
  • names.mdb --> MS-Access database

To execute the application, you must call flashasp.html through the web server. Because the database would be accessed via ASP and ASP requests must come through a web server, the flashasp.html must also be accessed accordingly. To test the application, use URL like:

http://localhost/flashdb/flashasp.html

where flashdb is the virtual directory where your application resides.

The code is fully commented and all the description is included within the sample. Please go through readme.txt.

Critical Code Comments

The most important code you would find in the Flash file is found in the Actions panel of the Find it command button. It looks like this:

   on (release) {

    loadVariablesNum("data.asp?Record=" add myrecord, 0);

    myrecord="";

    firstName="";

    lastName="";

    City="";

    errorMsg="Please wait";

   }

The second statement loadVariablesNum("data.asp?Record=" add myrecord, 0); is the first thing to notice. To interact with Flash via ASP, we need to pass the variables in a querystring. So basically, what we are doing is calling data.asp and sending the querystring Record to it. This would send whatever value the user types in the search field.

Once the data is sent in the querystring, the querystring value is received by ASP using record=request("Record"). The remaining ASP code just deals with connecting to the database, creating a RecordSet object, filling it up, and sending the data back to Flash.

Once ASP is finished retrieving the data, you would again need your old friend, the querystring. The following ASP code appends the querystring and passes it to Flash:

Response.Write "errorMsg=Found&firstName=" & _
  server.URLEncode(objrecord.fields("firstName")) & "&" & _
  "lastName=" & server.URLEncode(objrecord.fields("lastName")) & "&" & _
  "City=" & server.urlencode(objrecord.fields("City"))

You must remember that all the message passing between Flash and ASP must be done through the querystrings. If querystrings are not created properly, the application would not work.

How you can extend the code

The bottom line is this: all programming logic resides within the ASP script. If you are familiar with ASP, creating a full fledged database application - with update, delete, insert queries - is a piece of cake. All you need to do is pass the values between Flash and ASP using querystrings.

The advantage of using Flash at presentation tier is the extreme techno-savvy and professional look of the application while retaining a rather small file size.

Painful Lessons

A loose querystring, or rather, an improper querystring, would cause the application to fall flat on its face. Flash just doesn't care whether your application logic is free of all bugs - it would only accept values in a querystring. After hours and hours of trying to figure out what part of ASP needed a re-patch, it turned out that the querystring was mal-formed. Hence, Flash was unable to retrieve the values from ASP. You guys must watch out for this!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here