Flash_and_Flex_03_2009

Server Side Interaction

Listing 5. Writing Web Service Method in ASP.NET

//set the location of the wsdl public static const ENDPOINT_URL:String = "../StructuredDataFrom Inputs.asmx?wsdl"; //add the function for creation complete and //create the objects and methods for the interface private function initApp():void { gateway.wsdl = ENDPOINT_URL; gateway.loadWSDL(); gateway.GetStructuredDataForAdvancedDataGrid.a ddEventListener( ResultEvent.RESULT, recieveStructuredDataForAdvancedDataGrid); gateway.addEventListener(FaultEvent.FAULT, faultHandler); gateway.GetStructuredDataForAdvancedDataGrid(); //function for receiving the values from the web method private function recieveStructuredDataForAdvancedDataGrid(e: ResultEvent):void { var table:* = e.result.Tables.Table0.Rows; MyStructuredDataFromInputs = table; gc1.source = MyStructuredDataFromInputs; gc1.refresh(); Alert.show("Data recieved"); } //function for receiving the information of an error exception public function faultHandler(e:FaultEvent):void { Alert.show("An unexpected error occurred and has been logged." + e.message); } } //imports for the Alert/MessageBox import mx.controls.Alert;

//my custom web methods [WebMethod] public DataTable GetStructuredDataForAdvancedDataGrid() { try { string SSSql = "SELECT [Category], [Description], [Input] FROM StructuredDataFromInputs"; using (DbConnection cnn = ABCBusiness.CreateConnec tion()) { conn.Open(); DataSet ds = new DataSet(); SqlCommand command = conn.CreateCommand(); command.CommandText = SSSql; SqlDataAdapter da = new SqlDataAdapter(command); DataTable result = new DataTable("Table0"); da.Fill(result); return result;

}

} catch (Exception ex) {

throw CreateSafeException(ex);

}

}

Listing 6. Defining the Web Service Object, Events and Functions in FLEX

//gateway : this is the communication layer with the server side ASP.NET code private var gateway:WebService = new WebService();

Summary We have built a project, created an webservice using the Flex wizard, written a custom web method and integrated the data into an AdvancedDataGrid . Using this example as a structure can help you make the transition from the conventional programming technologies fromMicrosoft.

Now run the application and you will see the following see (Figure 7).

layout="absolute" creationComplete ="initApp()">

MITCHELL M. MAYNARD Mitchell Maynard is Senior Software Engineer for MDL ASSOCIATES LLC. His company specializes in developing enterprise applications in the financial services vertical. mitchell.maynard@hotmail.com

Figure 7. Your final result

03/2009 (5)

76

Made with FlippingBook HTML5