Search Flex Components Free

Custom Search

December 17, 2007 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) { components.Dispose();} base.Dispose(disposing); } [WebMethod] [XmlInclude(typeof(Member))] public Member[] getMembers() { ArrayList al = new ArrayList(); Member mem = new Member(0,"bob","bsmith","password1", "","Initiate",false,DateTime.Now); al.Add(mem); Member mem1 = new Member(0,"jim","jsmith","password2", "","Member",false,DateTime.Now); al.Add(mem1); Member mem2 = new Member(0,"ed","esmith","password3", "","Officer",false,DateTime.Now); al.Add(mem2); Member mem3 = new Member(0,"neil","nsmith","password4", "","Guest",false,DateTime.Now); al.Add(mem3); Member[] outArray = (Member[])al.ToArray(typeof(Member)); return outArray; } } [Serializable] 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:
package{ [Managed] [RemoteClass(alias="")] 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:

Related Flex Tutorials