Monday, October 02, 2006 C# data webservice for Flex

This is kinda the next evolutionary step from my previous blog. In this example I demonstrate how to get an array of records from .net to flex. Again, I have simplified this for the purpose of this blog. In the webservice, I would normally retrieve records from the database and then iterate thru the dataset to build my array, while here in this example I have just populated the array manually.

C# Webservice:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Xml.Serialization;

namespace Director
public class MemberService : System.Web.Services.WebService
public MemberService() { InitializeComponent(); }
private IContainer components = null;
private void InitializeComponent() {}
protected override void Dispose( bool disposing ) {
if(disposing && components != null) {

public Member[] getMembers()
ArrayList al = new ArrayList();
Member mem = new Member(0,"bob","bsmith","password1",
Member mem1 = new Member(0,"jim","jsmith","password2",
Member mem2 = new Member(0,"ed","esmith","password3",
Member mem3 = new Member(0,"neil","nsmith","password4",
Member[] outArray = (Member[])al.ToArray(typeof(Member));
return outArray;

public class Member
public int memberid;
public String name;
public string username;
public string password;
public string email;
public string comments;
public bool disabled;
public DateTime created;

public Member(int _memberid, string _name,
string _username, string _password,
string _email, string _comments,
bool _disabled, DateTime _created)
memberid = _memberid;
name = _name;
username = _username;
password = _password;
email = _email;
comments = _comments;
disabled = _disabled;
created = _created;
public Member(){}

In Flex, the file:
public class Member
public var memberid:int;
public var name:String;
public var username:String;
public var email:String;
public var password:String;
public var comments:String;
public var disabled:Boolean;
public var created:Date;

public function Member(obj:Object = null)
if (obj != null)
this.memberid = obj.memberid; =;
this.comments = obj.comments;
this.username = obj.username; =;
this.disabled = obj.disabled;
this.created = obj.created;
this.password = obj.password;
In Flex, the application:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="" layout="vertical">
import mx.collections.ArrayCollection;

private var myMembers:ArrayCollection;

private function resultHandler(event:ResultEvent):void {
myMembers = event.result as ArrayCollection;
for (var i:int=0; i<myMembers.length; i++) {
var currentMember:Member = new Member();
currentMember = new Member(myMembers.getItemAt(i));
myMembers.setItemAt(currentMember, i);
<mx:WebService id="myService"

<mx:DataGrid dataProvider="{myMembers}"/>


Stephen Sinclair said...


Good example, what if you turned on basic authentication and had to send credentials?
-Thanks, Steve

Anonymous said...

great article ! thanks !

Joel Lawler said...

Great stuff!!!

Thanks for posting this.

Anonymous said...

Cool posting, however, I'm getting an " could not be found" and " could not be found" errors. Each are displaying twice.

Any ideas as to the cause/solution?

Vic Rubba said...

the [Managed] tag on the class implies that you are using FDS or LCDS, either way you will need to include the fds.swc in your library path for the project. This library comes with the LCDS, available for download from Adobe. There's a free version for developers.

Anonymous said...


Thanks for the reply... What if you don't have that installed? Is there a way around it? What are the benefits?

Vic Rubba said...

Way back when I did this stuff I was using FDS, since that was the easiest way at the time to hook Flex to Java. Since then Adobe has released BlazeDS, which is open source, and it has many of the great features that FDS had. [Managed] implies that you will be using DataService, which allows for push of data to the browser, among other things. You can remove the [Managed] and use RemoteObject calls instead with BlazeDS to get traditional asynchronous data pull functionality from Java. Or, if BlazeDS doesn't float your boat, you can use straight Webservice calls. However, AMF serialization that you get with Blaze is pretty sweet and worth looking into.

Anonymous said...

wow! after installing Adobe Datalife Data Cycle and include fds.swc file, the sample code works! thanks for this wonderful post. helpe me a lot.

cesar.qüeb said...

Nice, But... How to use the service when this is published?


Anonymous said...

Please write a emample to pass a complex object in webservices. Simple parameter works fine but complex one is not working.