View comments | RSS feed

Using ADO.NET objects with Flash Remoting MX

Flash Remoting MX provides a service adapter for binding ADO.NET DataTables and DataViews to the Flash Remoting custom server control. To bind data sets to the custom server control, you use the control's DataSource property and DataBind method. In ActionScript, the results are exposed as a RecordSet object.

The following C# example could be used in a code-behind file or in the ASPX page that contains the Flash Remoting server control, as the following example shows:

<%@ Page Language="c#" Debug="true" %>
<%@ Register TagPrefix="Macromedia" Namespace="FlashGateway" Assembly="flashgateway" %>
<Macromedia:Flash id="Flash" Runat="Server" />
<%
  //create a SQL connection object and open a connection
  String source1 = "server=(local)\\NetSDK;" + "id=QSUser;pwd=QSPassword;" + "database=Northwind";
  sqlConnection = new SqlConnection(source1);
  sqlConnection.Open();

  //create the SQL statement
  String selectCountry = "SELECT DISTINCT Country FROM Customers ORDER BY Country ASC";

  //query the database
  SqlDataAdapter countryAdapter = new SqlDataAdapter(selectCountry, sqlConnection);

  //create a dataset object
  DataSet countryData = new DataSet();

  //fill the dataset with the query results
  countryAdapter.Fill(countryData, "Customers");

  //assign the dataset into the flash.datasource property
  Flash.DataSource = countryData.Tables["Customers"];

  //bind the datatable to the custom server control
  Flash.DataBind();

  //close the SQL connection
  sqlConnection.Close();
%>

In the code, the countryData DataSet is created from a SQL query to a database. Next, the countryData DataSet object is assigned into the Flash.DataSource property. Finally, the DataSet object is bound to the Flash Remoting custom server control using the Flash.DataBind method.

DataTables are serialized by Flash Remoting MX to a RecordSet in ActionScript. DataSets, which are collections of DataTables, are serialized by Flash Remoting MX to an associative array of RecordSets back in ActionScript.

The following Visual Basic .NET example shows the Page_Load method definition in a code-behind file that performs the same operation as the previous code example:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
  'create a SQL connection object and open a connection
  Dim source1 As String
  source1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data
    Source=C:\\inetpub\\wwwroot\\flashremoting\\Samples\\ado\\Northwind.mdb;"
  sqlConnection = New OleDb.OleDbConnection()
  sqlConnection.ConnectionString = source1
  sqlConnection.Open()
  'create the SQL statement
  Dim selectAll As String
  selectAll = "SELECT ContactName, City, Phone, Country FROM Customers"
  
  'Initialize a DataSet contactData and string selectContactData
  Dim contactData As New DataSet()
  Dim selectContactData As String
  
  'Check for parameters from Flash
  If (Flash.Params.Count = 0) Then
    'create the SQL statement
    Dim selectCountry As String
    selectContactData = "SELECT DISTINCT Country FROM Customers ORDER BY Country ASC"
  Else
    Dim selectedCountryName As String
    'assign parameter passed from Flash to variable
    selectedCountryName = Flash.Params(0).ToString()
    Dim selectContactData As String
    'insert Flash parameter into SQL statement
    selectContactData = "SELECT ContactName, City, Phone FROM Customers WHERE Country = \'" + selectedCountryName + "\'"
  End If

  'create the data adapter object
  Dim countryAdapter As System.Data.OleDb.OleDbDataAdapter

  'create a dataset object
  countryAdapter = New System.Data.OleDb.OleDbDataAdapter(selectAll, sqlConnection)

  'fill the dataset with the query results
  countryAdapter.Fill(contactData, "Customers")

  'assign the dataset into the flash.datasource property
  Flash.DataSource = contactData.Tables("Customers")

  'bind the datatable to the custom server control
  Flash.DataBind()

  'close the SQL connection
  sqlConnection.Close()
End Sub

In the code, the contactData DataSet is created from a SQL query to a database. Next, the contactData DataSet object is assigned into the Flash.DataSource property. Finally, the DataSet object is bound to the Flash Remoting custom server control using the Flash.DataBind method and returned to Flash.

Displaying a RecordSet in Flash with ActionScript

To display a RecordSet in a Flash UI component, you can use the DataGlue ActionScript file, which is installed with the Flash Remoting Components. You must first import the ActionScript file into your Flash application with the include directive, as the following example shows:

#include "DataGlue.as"

You can use the DataGlue.bindFormatStrings function to display the RecordSet in a Flash UI component, such as a ComboBox or a ListBox. The following example binds the result RecordSet to the displayNames ListBox UI component:

DataGlue.bindFormatStrings(displayNames, result, "#ContactName#", "#customerID#");

In the code, the last two arguments passed to the function (#ContactName# and #customerID#) are RecordSet column names. The ContactName column is displayed in the UI component, while the customerID column is returned when a user selects a particular record in the component.

The following ActionScript code connects to an ASPX page, returns a RecordSet, and displays the RecordSet in the displayNames ComboBox UI component:

//import the ActionScript classes
#include "NetServices.as"
#include "DataGlue.as"
//get a reference to the ASPX-based service
if (inited == null)
{
  inited = true;
  NetServices.setDefaultGatewayUrl("http://localhost/myASPApp/default.aspx");
  gatewayConnection = NetServices.createGatewayConnection();
  ASPXservice = gatewayConnnection.getService("myASPApp", this);
  //call the ASPX page
  ASPXservice.myASPPage();
}
//handler for ASPX page results
function myASPPage_Result(result) 
{                
  DataGlue.bindFormatStrings(displayNames, result, "#ContactName#", "#customerID#");
} 

You can also use the DataGlue.BindFormatFunction function to create custom formatting for your RecordSets. For more information on displaying RecordSets in ActionScript, Chapter 3, "Using Flash Remoting Data in ActionScript".

Comments


ctgi said on Dec 1, 2003 at 10:14 AM :
This does not work. I have tried this and some variations to no avail. Has anybody been able to get this to function?
jrunrandy said on Dec 1, 2003 at 10:19 AM :
LiveDocs is primarily for documentation comments and doesn't get enough page hits to answer a question like this. Try posting your question to the online forums: http://webforums.macromedia.com/flash/categories.cfm?catid=250
Michael Neptune said on Feb 26, 2004 at 2:33 PM :
No. I have found that you can not use DataGlue with any of the new UI2 components.

I have had luck with AMP returns of compiled C# sharp code in flash mx remoting only with the older controls.

Furthermore, the problem seems to be with how the dataset is returned.
I have not tried using the Flash object to bind data within the compile dll. I am going to try that next and let everyone know.
No screen name said on Mar 3, 2004 at 12:44 PM :
It is not well explained how a .NET DataSet can be used in an actionscript using Flash Remoting. The data comes down as an associated array. Is there and explanation on how to use this information in Flash?

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flashremoting/mx/Using_Flash_Remoting_MX/usingFRNET4.htm