Captivate Tutorial: Viewing a ColdFusion Query in a Flex DataGrid

I have long awaited for this day to post my first Captivate tutorial. The first topic that I am presenting on is one that I struggled with when I was first learning Flex 2 and also one that I know many other developers have an issue grasping. It is the subject of bringing in a basic ColdFusion query to a Flex application and showing it in a DataGrid. It is really not all that hard, but unless you know how to do it, there is really no way in "figuring it out".

I decided a Captivate tutorial would demonstrate this best, so without further delay, here is the tutorial URL: http://www.kylehayes.info/captivate/QueryToDatagrid/QueryToDataGrid.htm

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Nice demo!
# Posted By Holly | 4/17/07 7:48 AM
Glad to here you like it. If it is received well I will be posting more like this.
# Posted By Kyle Hayes | 4/17/07 7:51 AM
Thank you, thank you, thank you. This was very useful!
# Posted By Mike H | 4/17/07 7:57 AM
Great to hear! Keep the comments coming!
# Posted By Kyle Hayes | 4/17/07 8:10 AM
Excellent! Thanks for putting that together, that gave me some great ideas.
# Posted By RobW | 4/17/07 9:54 AM
My pleasure. Hope to have more coming in the near future. Please post suggestions!
# Posted By Kyle Hayes | 4/17/07 10:05 AM
Finally... Someone has created a very easy tutorial. I can't tell you how hard it is to find something like this out on the Web and now it's finally here!!! Thanks Kyle!
# Posted By Bill | 4/17/07 10:29 AM
I agree very nice and simple demo... thanks!!!
# Posted By Steini Jònsson | 4/18/07 1:46 AM
Yes, very good. More please...
# Posted By Josen Ruiseco | 4/18/07 9:24 AM
What editor are you using in eclipse for coldfusion?
# Posted By Scott | 4/19/07 1:46 PM
I use the CFEclipse plugin for Eclipse to edit ColdFusion
# Posted By Kyle Hayes | 4/19/07 2:56 PM
cool. I will have to get that. In any case, your example assumes that the destination="coldfusion" is set up? It's not in my config files and I can't get it to work! I've been trying to figure out where I can get a fresh start on my config files but haven't found any obvious sites.
# Posted By Scott | 4/19/07 3:01 PM
Thanks Kyle, very concise article and demo. Maybe as a logical next step you could do a data drill down on this sample, or maybe a data update/delete?
# Posted By Stefan le Roux | 4/19/07 9:33 PM
@Scott: Yes, it is assuming that your destination="ColdFusion" which is automatically setup in your services.config file on the ColdFusion server. If you have ColdFusion 7.02 installed you are all set. You just need to choose ColdFusion Flash Remoting inside of Flex Builder.

@Stefan: Thank you, and I will add your suggestions to the list!
# Posted By Kyle Hayes | 4/19/07 10:50 PM
Kyle (my hero),
I agree that people would really appreciate seeing how you can get a successful add/update/delete with the given example. This is for sure where I want to be heading!

Is there a reason that you have chosen remoting instead of Data Services? It seems like Data Services would be amazing if one could ever get them to work b/c of the possibility of having the real time data updates as changes are made.

As we speak I'm downloading the Coldfusion update 7.02 in hopes that it will fix the isssue!

Have you considered doing some sort of explanation about the .config files that you need for remoting and data services? I think people might appreciate that.
# Posted By Scott Hoff | 4/20/07 6:14 AM
Add / Update / Delete is in the works. This would especially be good since that is exactly what the ColdFusion wizard does. It would be great to see how to do it manually so that you actually know what is going on yourself.

As for the Data Services comment, the reason why I use remoting is because I can afford it ;-). You see Flex Data Services is a very expensive product (somewhere around $20k I believe). Granted there is a lite version. But remoting is very useful when you don't require live data updating. You can still do a time based server pull without remoting. Remoting is really great if you need to have server push, like a chat messaging client.

I think it is safe to assume that I will be doing a tutorial sometime in the future on how to work with Flex Data Services ;-)
# Posted By Kyle Hayes | 4/20/07 7:12 AM
Hey Great Tutorial, simple and to the point. If you do make anymore I have been looking for a tutorial on calling multiple cffunctions from the one remoteobject in flex. Thanks Again
# Posted By Joe G | 4/20/07 7:21 AM
Very nice. I could have saved myself some time had this been around a couple of months ago. One of the things I struggled with was where to do the data formatting of the output columns. Things like dates, dollar values with commas, etc. and then still allowing proper sorting. I ended up doing some in CF and some in Flex. I'd like to see your take, via example of doing some of this. Thanks!
# Posted By steveeray | 4/20/07 7:53 AM
AFTER MONTHS OF CONFUSION I HAVE FINALLY GOT SOMETHING TO WORK!!!!!!!!!!!!!!

I'm really excited this! Are you sure it costs 20k? I have something that is called Data Services running on my computer. I thought I paid a few extra hundred bucks for it but thought I would want the capability. Do you think that's why it never worked? It was never installed to begin with?

In any case, kyle saved me by pointing out that I had written Coldfusion instead of ColdFusion and thus caused the whole thing not to work. I think I was also running the 7.0 instead of 7.02.

A time based server pull = just periodically re-downloading the dataset and refilling the grid? Makes sense, do you just do that with an actionscript timer?

Above you mentioned that we are recreating what the wizards do. Which of the wizards corresponds to what we are doing?

Active Record CFC
Bean CFC & DAO CFC
Flex Data Service Assembler CFC
# Posted By Scott Hoff | 4/20/07 8:14 AM
Isn't that a great feeling to get a technology as great as Flex working!!! It really makes you feel warm and fuzzy inside whent that data populates in the grid for the first time :-D

Yes, Scott, when I referred to the a time based server pull (or poll) it would basically be an ActionScript Timer event that calls the <strong>get</strong> function of the RemoteObject that is updating your dataset.

In response to the wizard question, we have not done what the wizards are doing yet, but it is in the works. The wizard that I am talking about is after you install the Flex Builder ColdFusion extensions (which the Flex Builder prompts you to do after you install Flex Builder) there is a wizard when you go to create a <strong>New Project</strong>. It allows you to create a CRUD style application based on a datasource. I actually often times have issues with it, but nonetheless, I would like to do a tutorial that shows how to do basic actions like that that interact with CF components.
# Posted By Kyle Hayes | 4/20/07 9:04 AM
I have also tried to work with the CRUD interface creator and up to now haven't been able to get it work properly. I was able to retrieve the data but I don't think I was getting the insert and updating to work.

If you look at the documentation that I found on Adobe's site for Coldfusion, "Using Coldfusion with flex" http://download.macromedia.com/pub/documentation/e..."
i think that is mostly focuses on data services?

If you were to add a row to your recordset, would you send an http post to server to take care of the include and then simply add a "row/set/set of variables" to the array collection? I guess that would work b/c it would reflect the change. I suppose you could also simply redownload the whole dataset, but I imagine that could be problematic if you were dealing with a huge dataset.

Thanks for explaining the periodic calls.

Do you recommend the wizard?
# Posted By Scott Hoff | 4/20/07 9:17 AM
@Scott: I would not recommend the wizard for someone that is wanting to learn about how to integrate CF with Flex. It does everything for you and so there is no reason to learn it.

As far as how you would do the insert, what you would do, is build a function in your CFC called say addArtist or something like that and define the appropriate arguments such as firstName, lastName, email etc. Then you simply call the remoteObject just like you did to get records accept this time you are actually going to pass it some vars.

So if you had a form in your Flex app that had the variables that you wanted to pass to the CFC, then maybe you would do the following:

artistRO.create(txtFirstName, txtLastName, txtEmail);

and that would fire off the create method inside your artistRO object. It is pretty simple. But don't worry, I will show you how to do this in time with a Captivate example :-)
# Posted By Kyle Hayes | 4/20/07 9:24 AM
I tried copying and pasting the reference in the compiler to the .config file that I used in my functioning example so that I could apply the concept to an existing application in a project that up to now has only been using web services. However, after I inserted it I got this weird error:

The services configuration includes a channel-definition 'my-cfamf' that has an endpoint with a context.root token but a context root has not been defined. Please specify a context-root compiler argument.

I'm not 100% sure what it means. Why would the file be ok for one project and not be alright for another? Was adding the reference in the compiler the wrong way to add this functionality?
# Posted By Scott Hoff | 4/20/07 11:17 AM
Got it to work, but flex or cf is sorting the column order by the name of the columns... so If my sql query asked for firstname, lastname, internet ID, when this was displayed in the datagrid, it would display as : firstname, internetID, lastname. Anyway to make it not do it?
# Posted By Jose | 4/20/07 11:38 AM
Strange, the column order should match that of the query. Nonetheless, to ensure you have right order that you want you can do the following:

<mx:datagrid>
<mx:columns>
<mx:DataGridColumn headerText="First Name" dataField="firstName" />
<mx:DataGridColumn headerText="Last Name" dataField="lastName" />
<mx:DataGridColumn headerText="Email" dataField="email" />
</mx:columns>
</mx:datagrid>

The headerText can be whatever you want it to be. The datafield, is the column that you want to display.
# Posted By Kyle Hayes | 4/20/07 12:06 PM
@Randy, can you provide the your local filepath to the randypugh folder? (C:\inetpub\wwwroot...)
# Posted By Kyle Hayes | 4/22/07 8:09 AM
Yes, it's C:\CFusionMX7\wwwroot\CFIDE\randypugh
I'm not running IIS, as in your example.

Randy
# Posted By Randy | 4/22/07 8:20 AM
Yep, that's your issue. You need to have the randypugh directory inside wwwroot, not CFIDE.
# Posted By Kyle Hayes | 4/22/07 3:57 PM
Wow, thanks Kyle. That fixed it. For my own information, why would that make a difference?

I appreciate your help. I'll look to this site for more CF/Flex tutorials.
Randy
# Posted By Randy | 4/22/07 5:27 PM
I want to note that these tutorials are not necessarily the one right way to do things. I am just trying to help get people started. So the reason why you had to do it this way was because when ColdFusion looks for a component, it looks for it in different places. The first is the current directory that the calling object is in. Then the next place is the webroot. So in this case, the webroot is wwwroot, some sites, the folder may be "webroot" it just depends. Anyway, so because you were calling the component starting with your full name, that folder needed to be in the root. Otherwise, you would have had to change the starting folder to cfide.randypugh.cfcs.dataGateway. Also, I wouldn't recommend putting files in the CFIDE folder.

Hope this lengthy explanation helps :-)
# Posted By Kyle Hayes | 4/22/07 8:57 PM
cheers man! Great tutorial.
# Posted By sal | 4/23/07 8:09 AM
Glad to hear! Thanks!
# Posted By Kyle Hayes | 4/23/07 8:13 AM
Kyle,
Did you ever address why you think data services would cost 20,000 dollars??
# Posted By Scott Hoff | 4/23/07 1:54 PM
I did not, Scott. Thanks for reminding me. Here is the direct URL:
http://www.adobe.com/products/flex/productinfo/buy...

NOTE: Enterprise edition is $20,000 and Department edition (100 Users) is $6,000
# Posted By Kyle Hayes | 4/23/07 2:17 PM
Kyle,
I'm sorry to stick you with that. I thought it was you that told that to me. I guess someone must have told it to me because the 20,000 dollar number does match up to something. I just bought the Adobe Flex 2 training from the source and am trying to check out how that works.

One thing that remains unclear to me, is that if I always test my programs with the applications running on localhost, will it be a huge pain when I want to deploy the application to a different server? Have you ran into that yet?

P.S.
I've only had it for a few hours but I don't think they supply a tutorial like the one that you supply with the simple task of returning a query. I think they pretty much follow the path of starting you off with http requests and calling XML files from servers. This works fine until you have to start pushing the data back... The only way I could think of tackling that issue was to post to "web services" or coldfusion files that can do inserts and updates based on information that you would push via http via a query string. Oh... the possibilities.
# Posted By Scott Hoff | 4/23/07 2:23 PM
@Scott: There is a free personal / developer edition of Flex Data Services 2 that you can download from the same link that I posted in my last comment.
# Posted By Kyle Hayes | 4/23/07 2:36 PM
So Here's the deal. do you know what the limit on concurrent users is for flex data services when you use the developer edition? Should someone who is not ever going to have more avoid using dataservices together? I mean, if there is a limit on concurrent users, would 20 people using your flex app that used data services to push changes to the server cause your application to crash for those that come after the first 20 (or whatever the limit is)?
# Posted By Scott Hoff | 4/24/07 1:54 PM
Awesome tutorial!

You describe in a couple of minutes what it took me several of days to get working.
Really looking forward to the CRUD tutorial.

//Morgan
# Posted By Morgan Gustafsson | 5/13/07 4:07 AM
Thank you very much for posting this tutorial. What a help to us beginners. I have closely followed your example (careful with upper and lowercase, spelling etc.... but for some reason my grid just comes up empty with no columns and no data. Just one big empty column. no errors though... anyone have an idea? Thanks for any pointers.
Jeff
# Posted By Jeff | 5/19/07 8:00 AM
Greate example. I'm hoping you or someone can help. I followed your steps to the letter and got this error when I tried to Run as a Flex application: Errors exits in required project. Any ideas what causes this?
# Posted By Derrick | 7/25/07 10:39 AM
HIt your Errors tab in Eclipse and see what errors are being reported
# Posted By Kyle Hayes | 7/25/07 11:09 AM
Hi Kyle. Thanks for the quick response. I took a look at this earlier and per the problems tab here is the error: Unexpected attribute 'url' found in 'endpoint' from file services-config.xml

After installing the plugin was there some other configuration that I needed to do? BTW I'm working on a MAC so I'm not sure if that changes things.
# Posted By Derrick | 7/25/07 11:13 AM
That shouldn't make a difference as I run on Mac as well. The only reason the tutorial is on a PC is because that is what Captivate runs on ;-)

Anyway, as for your error, it does have to do with your services-config file. Would you mind posting the text of that file at http://kylehayes.privatepaste.com/xml/ and then posting the link here? If you don't want anyone else to view it later, make sure you set an expiration. Thanks.
# Posted By Kyle Hayes | 7/25/07 11:16 AM
Kyle,
Here is the link you asked for: http://kylehayes.privatepaste.com/ca19to4QGx

Thanks again.
# Posted By Derrick | 7/25/07 11:25 AM
A little more information. I'm using CF8 Beta (multi-servier configuration) and Apache
# Posted By Derrick | 7/25/07 11:26 AM
Do you get a blank page when visiting http://{yourserverroot}/flex2gateway/ ?
# Posted By Kyle Hayes | 7/25/07 1:15 PM
I do get a blank page now (JUST got that working). I removed and reinstalled CF 8 and that got rid of the services-config.xml problem. Now I think I'm getting closer. When I try to run the app I get the same error that folks on this post are getting: http://www.adobe.com/cfusion/webforums/forum/messa...

I just upgraded Flash Player to see if that is the problem and the error doesn't pop up now, but I only get a blank flex screen. No datagrid or anything. But I AM getting closer.
# Posted By Derrick | 7/25/07 1:24 PM
i get a page cannot be displayed when i go to Do you get a http://{yourserverroot}/flex2gateway/ ?
it was working before now it's not, can help me to get it back to working?
# Posted By Chaz | 8/16/07 1:29 PM
It would be very useful if you could write a quick article with code snipits or supply a basic demo app.
Watching on video, which takes forever to load is not very "usable".
However, your demo "title" interests me so i shall continue waiting...
# Posted By Joe | 8/21/07 1:21 AM
Based on the current number of positive responses, I think this method is extremely usable. Thanks for posting your opinion though.
# Posted By Kyle Hayes | 8/21/07 6:19 AM
What a great tutorial video for beginers like me. I would love to see more tutorial like this on how to do some basic records maintenance like being able to add, delete and edit records and pass it it back to the grid.
# Posted By Sam | 9/6/07 3:46 AM
Yes, my time after work is starting to open up again so I am really hoping to be able to some more very soon.
# Posted By Kyle Hayes | 9/6/07 6:33 AM
I can't get the tutorial to work. I get 'acArtists is undefined'. It's like it can't 'see' the array.
I have the latest version of ColdFusion 7 and Flex 2.0. Any ideas what I am doing wrong? HELP?
# Posted By Lara | 10/2/07 11:17 AM
@Lara: Did you try to do some line by line debugging?
# Posted By Kyle Hayes | 10/6/07 5:57 PM
Great tutorial/demo.

Thanks
# Posted By Steve | 10/16/07 2:27 PM
When I try to create the project it is telling me Invalid root. A config file must exist in WEB-INF/flex. I have the standalone coldfusion mx7 server installed. Do I have to install something for flex?
# Posted By Chris | 10/25/07 11:48 AM
Are you running CFMX 7.02 (emphasis on the '02')?
# Posted By Kyle Hayes | 10/25/07 11:51 AM
Turns out I am running 7,0,0,91690. I'll update to 7.02 and try again. Thanks.
# Posted By Chris | 10/25/07 11:55 AM
Thank you so much for the captivate tutorial. We are trying to pick up flex here at work, but without paying for classes. Books and their info are so limited out there, and specific questions usually go unanswered in forums. This issue in your tutorial was one we had questions about, so thank you for creating it. I wish you had more that answered some basic flex questions! Please consider making more. Thank you!
# Posted By donna | 11/14/07 7:55 AM
I currently use webservice calls to accomplish these kind of tasks, but I have been told remoteobject offers better performance. I don't understand how a flex app like this would be portable though. What would we have to tweak in this example if it were moved off localhost.
# Posted By Eric | 11/27/07 3:59 PM
You don't as long as the CFC dot path remains the same on your destination server. Granted your destination server needs to have CF 7.02 or higher on it.
# Posted By Kyle Hayes | 11/27/07 10:12 PM
Hi Derrik,

there is a hot fix from adobe for the bug 'unexpected url found'..
get the fix - hotfix 2 from adobe site
http://kb.adobe.com/selfservice/viewContent.do?ext...
# Posted By bibin | 2/14/08 6:11 AM
Thanks for making this tutorial, I have been searching for a long time and I finally got my datagrid to work! When you get the chance, I am fairly new to ColdFusion and looking for an example on how to use storedproc and displaying multiple columns to a datagrid would be a great example. Thanks again!
# Posted By Helen | 7/10/08 4:40 PM
I am getting a blank data grid on my machine... i am using coldfusion built in server to run the app.
# Posted By Pramir | 8/8/08 7:27 AM