Naveed Mansuri
Naveed Mansuri

Reputation: 459

flex webservice data cannot bind with flex datagrid: Flash Builder 4.7

I am trying to access data available in SQLServer in flex application through webservice.

I have wsdl document shown below:

<?xml version="1.0" encoding="UTF-8"?>
 -<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://tempuri.org/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tempuri.org/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"> -<wsdl:types> -<s:schema targetNamespace="http://tempuri.org/" elementFormDefault="qualified"> -<s:element name="GetEmployees"> <s:complexType/> </s:element> -<s:element name="GetEmployeesResponse"> -<s:complexType> -<s:sequence> <s:element name="GetEmployeesResult" type="tns:ArrayOfEmployee" maxOccurs="1" minOccurs="0"/> </s:sequence> </s:complexType> </s:element> -<s:complexType name="ArrayOfEmployee"> -<s:sequence> <s:element name="Employee" type="tns:Employee" maxOccurs="unbounded" minOccurs="0" nillable="true"/> </s:sequence> </s:complexType> -<s:complexType name="Employee"> -<s:sequence> <s:element name="EmpId" type="s:string" maxOccurs="1" minOccurs="0"/> <s:element name="EmpName" type="s:string" maxOccurs="1" minOccurs="0"/> <s:element name="EmpEmail" type="s:string" maxOccurs="1" minOccurs="0"/> </s:sequence> </s:complexType> -<s:element name="SaveEmployee"> -<s:complexType> -<s:sequence> <s:element name="empId" type="s:string" maxOccurs="1" minOccurs="0"/> <s:element name="empName" type="s:string" maxOccurs="1" minOccurs="0"/> <s:element name="empEmail" type="s:string" maxOccurs="1" minOccurs="0"/> </s:sequence> </s:complexType> </s:element> -<s:element name="SaveEmployeeResponse"> <s:complexType/> </s:element> </s:schema> </wsdl:types> -<wsdl:message name="GetEmployeesSoapIn"> <wsdl:part name="parameters" element="tns:GetEmployees"/> </wsdl:message> -<wsdl:message name="GetEmployeesSoapOut"> <wsdl:part name="parameters" element="tns:GetEmployeesResponse"/> </wsdl:message> -<wsdl:message name="SaveEmployeeSoapIn"> <wsdl:part name="parameters" element="tns:SaveEmployee"/> </wsdl:message> -<wsdl:message name="SaveEmployeeSoapOut"> <wsdl:part name="parameters" element="tns:SaveEmployeeResponse"/> </wsdl:message> -<wsdl:portType name="ServiceSoap"> -<wsdl:operation name="GetEmployees"> <wsdl:input message="tns:GetEmployeesSoapIn"/> <wsdl:output message="tns:GetEmployeesSoapOut"/> </wsdl:operation> -<wsdl:operation name="SaveEmployee"> <wsdl:input message="tns:SaveEmployeeSoapIn"/> <wsdl:output message="tns:SaveEmployeeSoapOut"/> </wsdl:operation> </wsdl:portType> -<wsdl:binding name="ServiceSoap" type="tns:ServiceSoap"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/> -<wsdl:operation name="GetEmployees"> <soap:operation style="document" soapAction="http://tempuri.org/GetEmployees"/> -<wsdl:input> <soap:body use="literal"/> </wsdl:input> -<wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> -<wsdl:operation name="SaveEmployee"> <soap:operation style="document" soapAction="http://tempuri.org/SaveEmployee"/> -<wsdl:input> <soap:body use="literal"/> </wsdl:input> -<wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> -<wsdl:binding name="ServiceSoap12" type="tns:ServiceSoap"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/> -<wsdl:operation name="GetEmployees"> <soap12:operation style="document" soapAction="http://tempuri.org/GetEmployees"/> -<wsdl:input> <soap12:body use="literal"/> </wsdl:input> -<wsdl:output> <soap12:body use="literal"/> </wsdl:output> </wsdl:operation> -<wsdl:operation name="SaveEmployee"> <soap12:operation style="document" soapAction="http://tempuri.org/SaveEmployee"/> -<wsdl:input> <soap12:body use="literal"/> </wsdl:input> -<wsdl:output> <soap12:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> -<wsdl:service name="Service"> -<wsdl:port name="ServiceSoap" binding="tns:ServiceSoap"> <soap:address location="http://localhost:28408/TestWebService/Service.asmx"/> </wsdl:port> -<wsdl:port name="ServiceSoap12" binding="tns:ServiceSoap12"> <soap12:address location="http://localhost:28408/TestWebService/Service.asmx"/> </wsdl:port> </wsdl:service> </wsdl:definitions>

And my code for mxml page is:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView"
    creationComplete="view1_creationCompleteHandler(event)">

<fx:Declarations>

    <s:WebService id="ws" wsdl="http://localhost:28408/TestWebService/Service.asmx?wsdl" fault="fault(event)">
        <s:operation name="GetEmployees" 
                     resultFormat="object"
                     result="GetEmployees(event)">

        </s:operation>
        <s:operation name="SaveEmployee" 
                     resultFormat="object"
                     result="SaveEmployee(event)">

        </s:operation>
    </s:WebService>

</fx:Declarations>

<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;
        import mx.rpc.events.FaultEvent;
        import mx.rpc.events.ResultEvent;
        import mx.collections.ArrayCollection;

        [Bindable]
        public var dataArray:ArrayCollection = new ArrayCollection();

        protected function view1_creationCompleteHandler(event:FlexEvent):void
        {
            // TODO Auto-generated method stub
            ws.GetEmployees();
        }

        private function GetEmployees(event:ResultEvent):void {
            // Databind data from webservice to datagrid
            dataArray = event.result;
        }

        private function SaveEmployee(event:ResultEvent):void {
            // To Refresh DataGrid;
            ws.GetEmployees(); 
            //Alert.show("Saved Successfully");
        }

        private function AddRecord(event:MouseEvent):void {
            // Save a record using a WebService method
            //ws.SaveEmployee(txtEmpId.text, txtEmpName.text, txtEmpEmail.text); // 
        }

        private function fault(event:FaultEvent):void {
            // Oppps some error occured
            flt.text = event.fault.toString();
        }

    ]]>
</fx:Script>

<s:DataGrid id="datagrid" dataProvider="{dataArray}" width="465" height="100%" visible="true">
    <s:columns>
        <s:ArrayList>
            <s:GridColumn headerText="Emp Id" dataField="EmpId"/>
            <s:GridColumn headerText="Emp Name" dataField="EmpName"/>
            <s:GridColumn headerText="Emp Email" dataField="EmpEmail"/> 
        </s:ArrayList>          
    </s:columns>
</s:DataGrid>
<s:TextArea x="500" id="flt" visible="true" width="300"/>

When I am trying to run this project I get error while binding data to datagrid like: 1118: Implicit coercion of a value with static type Object to a possibly unrelated type mx.collections:ArrayCollection.

Where am I wrong? Can anyone tell me?

Upvotes: 0

Views: 471

Answers (2)

flexicious.com
flexicious.com

Reputation: 1671

It appears that your event.result is not an ArrayCollection, but a plain object. You will need to insect event.result and figure out how to get your array collection from it.

        dataArray = event.result.someProperty;

OR

        dataArray = new ArrayCollection([event.result]); //if your service is just returning one item.

Upvotes: 0

vijaygurjar
vijaygurjar

Reputation: 67

You have to type cast event.result as event.result as ArrayCollection.

like private function GetEmployees(event:ResultEvent):void { dataArray = event.result as ArrayCollection; }

Regards, Vijay

Upvotes: 1

Related Questions