human growth hormone

Flex 3 Insert Record into a Database using Remote Object CFC ColdFusion

Written by wecodethings on May 10, 2009 – 8:07 pm

Here is a DEMO of the application if you would like to see the finished product:
Flex 3 Insert Record into a Database using Remote Object CFC ColdFusion

Here is how to setup and insert a record into a database using Flex 3 with Remote Object / Flash Remoting with ColdFusion CFC.

Here is my Remote Object:
<mx:RemoteObject id=”mycfc

                 destination=”ColdFusion

                 showBusyCursor=”true

                 source=”cfctest.thecfc

                 result=”result(event)”

                 fault=”fault(event)”>
<mx:method name=”insertrecord” result=”confirminsert(event)”/>

</mx:RemoteObject>

 I want to setup a method that will allow me to insert a record into the database.  In my CFC, I have a function called insertrecordthat simply inserts a record:

<cffunction name=“insertrecord” access=“remote” returntype=“void”>

      <cfargument name=“FirstName” type=“string” required=“Yes”>

      <cfargument name=“LastName” type=“string” required=“Yes”>

    <cfquery datasource=“mysqlcf_flexdemo”>

INSERT INTO tblTest (tstFirstName, tstLastName)

      VALUES

(<cfqueryparam value=“#arguments.FirstName#” cfsqltype=“cf_sql_varchar”>,

 <cfqueryparam value=“#arguments.LastName#” cfsqltype=“cf_sql_varchar”>)     

    </cfquery>

  <cfreturn>

 </cffunction>

 I then setup a function in ActionScript called “confirminsert” which will be my result of my method within my remote object.  Within “confirminsert”, I am going to pass the event. This function will get called after the Insert has taken place.

private function confirminsert(evt:ResultEvent):void

{

Alert.show(txtFirstName.text + ” “ + txtLastName.text + ” was inserted into the database”, “Insert Successful”);

     txtFirstName.text = “”;

     txtLastName.text = “”;

      mycfc.recordcount();

}

 

Before the confirminsert function gets ran, I like to setup a startinsert function that will do some error handling for us. This is the first function that will get called from the actual insert button. It first looks to make sure that both input boxes are not blank and if they are not blank, then initiate the insert by calling:

mycfc.insertrecord(txtFirstName.text,txtLastName.text); This line will actually insert the record, when this is called, the insert takes place and then the result is then run from our “confirminsert” function.

The errorString can be placed on any object and can give an instruction as to why the insert failed. If the insert was successful, then we need to clear the errorString by using txtFirstName.errorString = ‘ ‘; syntax.

private function startinsert(evt:Event):void

{

if (txtFirstName.text != “” && txtLastName.text != “”)

// All statements inside { } are guarded by IF.

           {

            mycfc.insertrecord(txtFirstName.text,txtLastName.text);

            txtFirstName.errorString = ;

            txtLastName.errorString = ;

            }

      else

            {

 Alert.show(“check to make sure you have entered both the first AND the last name”);

 txtFirstName.errorString = “First Name Cannot be Left Blank”;

 txtLastName.errorString = “Last Name Cannot be Left Blank”;

            }

}


Lastly, create first name and last name input boxes and a button with a click event calling the “startinsert(event)” function that we’ve already created.

 

<mx:HBox width=”100%” horizontalAlign=”center>

     <mx:Label text=”First Name:/>

     <mx:TextInput id=”txtFirstName/>

</mx:HBox>

<mx:HBox width=”100%” horizontalAlign=”center>

     <mx:Label text=”Last Name:/>

     <mx:TextInput id=”txtLastName/>

</mx:HBox>

<mx:HBox width=”100%” horizontalAlign=”center>

<mx:Button label=”Insert Name” id=”btninsert

           click=”startinsert(event)”/>

</mx:HBox>

 

 

Here is a DEMO of the application if you would like to see the finished product:
Flex 3 Insert Record into a Database using Remote Object CFC ColdFusion

DOWNLOAD files here:
CFC     - Download
Flex     - Download


Tags: , , , , , ,
Posted in ColdFusion, Flex, SQL | No Comments »

{literal} {/literal}