Updating a Database Using Flash MX Professional 2004
Table of Contents
- Introduction
- The Basics
- Update Packets in Detail
- Result Packets in Detail
- A Sample Update Packet Parser
- Setting up the ColdFusion Component
- Building the Flash Application
- Summary
Building the Flash Application
Now I'm going to show you how to build a simple application that makes use of the ColdFusion component to load data from the database and save data back to it.
- Open Flash MX Professional 2004 and create a new Flash document.
- Drag a DataSet component to the Stage and name it ds. Drag a DataGrid component to the Stage, name it grid, set its x,y position to 0,0, set its width to 540, and set its height to 240. In the Component Inspector, set its editable property to true.
Databind ds.dataProvider to grid.dataProvider by following these steps:
- Select the DataSet component on the Stage and choose the bindings tab in the Component Inspector.
- Click the + button to add a new binding.
- In the Add Binding dialog box, select the dataProvider property and click the OK button.
- In the Component Inspector, select the bound to property of the new binding, and click the button next to it.
- In the Bound To dialog box, select the DataGrid, <grid> item in the Component Path list box. You'll see a list of the DataGrid properties in the Schema Location list box on the right.
- Select the dataProvider property, and click the OK button to complete your task (see Figure 1).
Figure 1. Task completed: ds.dataProvider is bound to grid.dataProvider
- Databind ds.selectedIndex to grid.selectedIndex following in outline the directions in step 3.
Add schema to the DataSet component to describe the expected fields. Click the DataSet component, and select the Schema tab in the Component Inspector.
- Click the leftmost + button to add a component property. Type id into the field name text box, and select Number from the data type drop-down list. This tells the DataSet to expect an id field in the data, and that its value is a number.
- Following the instructions above, add the following fields and data types: rate as Number; duration as Number; billable as Boolean; notes as String (see Figure 2).
Figure 2. The finished schema of the DataSet component
Click the Stage to deselect all selected components and type the following code into the Actions panel (this code sets up a connection to the ColdFusion component and calls the getSQLData() function to get data):
#include "NetServices.as" #include "NetDebug.as" //Set up the NetServices connection to the ColdFusion component. var con:NetConnection = NetServices.createGatewayConnection( "" ); _global.srv = con.getService( "rdbmsProcessor", this ); //Call the function with a query to get the data. _global.srv.getSQLData( "timetrax", "select notes, billable, duration, rate, duration, id from timeentry" ); //Called with the result from the getSQLData function, this places the data into the dataset. function getSQLData_Result( result ) { ds.dataProvider = result; }
To add the ability to add new records, drag a Button control to the Stage, set its label property to Add and type the following code into the Actions panel:
on(click){ _parent.ds.addItem(); }
To add the ability to delete records, drag another Button control to the Stage, set its label property to Delete and type the following code into the Actions panel:
on(click){ _parent.ds.removeItem(); }
Test the movie. Verify that you are getting data in the grid and that you can add, edit, and delete records. At this point, there is no way to save your changes. If you can not modify records, review the steps above, and verify that you have ColdFusion MX set up correctly with the ColdFusion component and database by typing the following into your browser:*%20from%20timeentry
- To add the ability to save your changes, drag an RDBMSResolver component to the Stage, name it rs.
In the Component Inspector, and specify the following properties:
- Set the tableName property to TimeEntry.
- Click the button next to the fieldInfo property.
- In the Values dialog box, click the + button.
- In the area below, type id into the fieldName property, and click the OK button (see Figure 3).
Figure 3. Filling in the Values dialog box
This instructs the RDBMSResolver component that the id field is the key field for this table (see Figure 4).
Figure 4. The RDBMSResolver component configured
With the RDBMSResolver component still selected, type the following into the Actions panel:
on(beforeApplyUpdates) { //Add an attribute to the update packet to tell the CFC //that we want it to generate values for the id field var firstChild:XMLNode = eventObj.updatePacket.firstChild; firstChild.attributes.autoIncField = "id"; trace(eventObj.updatePacket); //Now send the update packet to the parsing method in the CFC _global.srv.saveSQLData( "timetrax", eventObj.updatePacket.toString()); }
- Databind rs.deltaPacket to ds.deltaPacket using in outline the directions in step 3.
Drag a Button control to the Stage, set its label property to Save, and type the following into the Actions panel:
on(click) { _parent.ds.applyUpdates(); }
To catch the result packet coming back from the CFC, click the Stage to deselect any selected components, and type the following code into the Actions panel:
//Catches the result packet returned by the CFC, and puts it into the resolver function saveSQLData_Result( result ) { trace(result); var resultDoc:XML = new XML(); resultDoc.ignoreWhite = true; resultDoc.parseXML(result); rs.updateResults = resultDoc; }
To add in the ability to view errors returned in the result packet, select the DataSet and type the following in the Actions panel:
on(resolveDelta) { var errs:Array = eventObj.data; for( var i:Number=0; i<errs.length; i++ ) { trace( errs[i].getMessage()); } }
That's it. You can now play around with the application. Try adding, editing, and deleting records. Take a look at the contents of the update packet and result packets in the output panel. Notice how the value for the id field is automatically created within the server function and how the DataSet picks up and displays the updated value in the grid.
Of course, you could also enhance the functionality of this application by adding the ability to deal with errors returned from the server. You can see a working example of error resolution in Paul Gubbay's Time Entry sample application.