Some experience with web programming using the .NET platform and C#, as well as the ability to set up and use databases with SQL Server is required. Prior experience with Flex will be helpful.
Beginning
This article describes how to use the data-centric development features of Flash Builder 4 to build a Flex application that accesses an SQL database created in Microsoft Visual Studio via a .NET-based web service. Web services are commonly used as a mechanism for data delivery on the Internet, and the .NET platform is widely used to implement web services. By following the steps in this tutorial, you will see how easy it is to retrieve data from a web service in a Flex application without writing any code.
The example database for this tutorial is for an imaginary courier company that has stored the locations of its offices in the form of a table (named Centre) with the following columns and data types (see Figure 1):
To access the data from the Centre table, you can use the sample files included with this tutorial, which implement a simple web service in C#. The code consists of three files:
getCentres() function retrieves all the tuples in the database and returns them as an array of Centre objects. The getCentreNames() function retrieves the list of CentreName attributes alone and returns them as an array of strings. The functions connect to the database using the System.Data.SqlClient namespace.Before you can start using the web service functions from a Flex client, you need to know a little bit about Web Service Description Language (WSDL). WSDL is a standard XML-based language used to describe or model web services.
When you create a web services in Visual Studio, the WSDL is automatically generated. If the web service is deployed on a server, then the path of the WSDL is in the following format unless specified otherwise by the user:
http://{serverpath}/{context of the web application}/{Name of the services file- for example, services.asmx}?WSDL
While debugging in Visual Studio the default WSDL path for Service.asmx in the project named CentreServices is:
http://localhost:4682/CentreServices/Service.asmx?WSDL
This WSDL path will be used in Flash Builder 4 to connect to the service.
You can use the Database.mdf file, which is included among this tutorial’s sample files, as the SQL Server database.
The code in Service.cs includes two path references that you’ll need to change. Search for AttachDbFilename=/*ENTER PATH OF THE DB HERE*/ in the file and edit the two instances as necessary for your configuration. Refer to comments within the file to format the path properly.
After setting up the web service code and the database on your local server, start Flash Builder 4 and follow these steps to define the web service:
The Service Name, Service Package, and Data Type Package are filled in automatically by Flash Builder 4. Since the name of the ASMX file is Service.asmx, Flash Builder 4 has given the Service Name the same value by default.
Flash Builder 4 will introspect the WSDL file and present a list of services, ports, and operations supported by the service.
getCentres() and getCentreNames() are selected.In the Data/Services view you’ll see a list of the different operations and data types that were identified for the service (see Figure 4).
In just a few simple steps, you defined the .NET-based web service. Flash Builder 4 introspected the service, automatically generated value objects, and assigned the correct return types to the service’s operations.
Now that you’ve set up the service in Flash Builder 4, you’re ready to connect it to the user interface of your Flex application:
getCentreNames():String[] as the operation (see Figure 5).
That’s it. Flash Builder 4 has generated all the code that’s needed to display the results of the service call in the List control.
getCentres():Centre[] operation from the Data/Services view and drop it on the DataGrid control. (This is an alternative to selecting the control, right-clicking, and selecting Bind To Data.)The value object for the corresponding Centre class written in C# was automatically generated by Flash Builder 4. Here, the value object is essentially the same class written in ActionScript. To see the generated code, expand the valueObjects package in Package Explorer and open the Centre.as file. This ActionScript class can be reused for other purposes as a strongly typed class.
Now that you have seen how easy it is to invoke a .NET-based web service from your Flex applications, you can use this approach to improve the richness of your web applications. With data-centric development you can even convert traditional web applications to Rich Internet Applications quickly and efficiently. For more information on data-centric development see the article Data-centric development with Flash Builder 4.
You could expand on this application in many ways. For example, you could use the data retrieved from the database to plot the location of offices (or even parcels) on a map. You can read more about how I developed a similar application using Google Maps on my blog.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License