15 Replies Latest reply on May 4, 2009 6:30 PM by ntsiii

    .Net WebService to fill a dataProvider

    max.soft

      Hi all,

      I've create a .Net WebService that returns an XMLDocument. (or a String XML formatted)

      I dont know how to pass the evt.result to a mx:DataGrid dataProvider.

      Can you help me?

       

      thank you

        • 1. Re: .Net WebService to fill a dataProvider
          Michael Borbor Level 4

          Hi there you assign your event.result to an XMLListCollection preferably, first you declare your variable.

           

          [Bindable]
          private var myDP:XMLListCollection;
          
          

          And then in the event result you assign the values to the variable

           

          myDP=new XMLListCollection(evt.result.MyMainNodeName);
          

          And finally you set in the DataGrid the dataProvider to myDP, or any name that you put to your variable

          • 2. Re: .Net WebService to fill a dataProvider
            max.soft Level 1

            hi,

            thank you for your help.

            It does not work properly.

            I attach the xml file and paste the mxml.

            can you suggest me the last step?

             

                    private function getData_result(evt:ResultEvent):void {
                        myDP = new XMLListCollection(evt.result.DocumentElement);
                         dataGrid.dataProvider = myDP;
                     }

             

             

            <mx:DataGrid id="dataGrid"></mx:DataGrid>

            • 3. Re: .Net WebService to fill a dataProvider
              Gregory Lafrance Level 6

              If this code answers your question or helps, please mark it as such.

               

              var xmllistObj:XMLList = evt.result..Customers;

              myDP = new XMLListCollection(xmllistObj);

              • 4. Re: .Net WebService to fill a dataProvider
                max.soft Level 1

                hi thank you but does not works.

                It does not show anything in the datagrid.

                need I to define DataGridColumns?

                • 5. Re: .Net WebService to fill a dataProvider
                  Gregory Lafrance Level 6

                  Yes, you need to define DataGridColumns, and the dataField properties should be set to the fields in your XML under the Customers tag to reference.

                   

                  See the Flex Builder help sys page on e4x parsing syntax as well.

                  • 6. Re: .Net WebService to fill a dataProvider
                    max.soft Level 1

                    hi,

                    this is the mxml and does not work:

                        <mx:DataGrid id="dataGrid" dataProvider="{myDP}">
                            <mx:columns>
                                <mx:DataGridColumn dataField="CustomerID"></mx:DataGridColumn>
                            </mx:columns>
                        </mx:DataGrid>

                     

                    and script:

                            private function getSessionID_result(evt:ResultEvent):void {
                                var xmllistObj:XMLList = evt.result.Customers;
                                myDP = new XMLListCollection(xmllistObj);
                            }

                     

                    please...

                    • 7. Re: .Net WebService to fill a dataProvider
                      Gregory Lafrance Level 6

                      Try two dots:

                       

                      var xmllistObj:XMLList = evt.result.Customers;

                      What error are you getting?

                      • 9. Re: .Net WebService to fill a dataProvider
                        max.soft Level 1

                        two dots give an error.

                         

                        can I show the xmlDocument? to see how wich type of data are stored in?

                        • 10. Re: .Net WebService to fill a dataProvider
                          Gregory Lafrance Level 6

                          I haven't used WebService, but here is a working example using HTTPService. As long as your web service is coming back as XML, and as long as you are parsing it correctly. It should work.

                           

                          <?xml version="1.0" encoding="utf-8"?>
                          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                            creationComplete="dataRequest.send();">
                            <mx:Script>
                              <![CDATA[
                                import mx.rpc.events.ResultEvent;
                                import mx.collections.XMLListCollection;
                               
                                [Bindable] private var myDP:XMLListCollection;
                               
                                private function getData_result(evt:ResultEvent):void {
                                  myDP = new XMLListCollection(evt.result..Customers as XMLList);
                                }
                              ]]>
                            </mx:Script>
                            <mx:HTTPService id="dataRequest" useProxy="false" resultFormat="e4x"
                              result="getData_result(event);" url="GetDataTableXML.xml"/>
                            <mx:DataGrid id="dataGrid" dataProvider="{myDP}">
                              <mx:columns>
                                <mx:DataGridColumn dataField="CustomerID"></mx:DataGridColumn>
                                <mx:DataGridColumn dataField="CompanyName"></mx:DataGridColumn>
                                <mx:DataGridColumn dataField="ContactName"></mx:DataGridColumn>
                                <mx:DataGridColumn dataField="ContactTitle"></mx:DataGridColumn>
                                <mx:DataGridColumn dataField="Address"></mx:DataGridColumn>
                                <mx:DataGridColumn dataField="City"></mx:DataGridColumn>
                                <mx:DataGridColumn dataField="PostalCode"></mx:DataGridColumn>
                                <mx:DataGridColumn dataField="Country"></mx:DataGridColumn>
                                <mx:DataGridColumn dataField="Phone"></mx:DataGridColumn>
                                <mx:DataGridColumn dataField="Fax"></mx:DataGridColumn>
                              </mx:columns>
                            </mx:DataGrid>
                          </mx:Application>

                          • 11. Re: .Net WebService to fill a dataProvider
                            max.soft Level 1

                            Thank you,

                            two dots give me an error:

                            Descendants (..) operator is not supported in Array type

                             

                            ... it's not possible that is not an official Example on how to use Net WebService and Flex

                            • 12. Re: .Net WebService to fill a dataProvider
                              Gregory Lafrance Level 6

                              It doesn't look like you are being returned XML data. What is in the array elements of the array that is being returned?

                              • 13. Re: .Net WebService to fill a dataProvider
                                max.soft Level 1

                                Well...

                                I've decided that is better use a String variable that contains an xml document.

                                 

                                Now I would like to know how to trasform a String variable to a dataProvider.

                                 

                                do you know?

                                 

                                thank you

                                • 14. Re: .Net WebService to fill a dataProvider
                                  Michael Borbor Level 4

                                  Why would you want to do that, don't. This is a complete app using your XML as source.

                                   

                                  <?xml version="1.0" encoding="utf-8"?>
                                  <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
                                                                backgroundAlpha="1" creationComplete="myService.send()">
                                       <mx:Script>
                                            <![CDATA[
                                                 import mx.controls.Alert;
                                                 import mx.collections.XMLListCollection;
                                                 import mx.rpc.events.ResultEvent;
                                                 [Bindable]
                                                 private var myXMLCollection:XMLListCollection;
                                                 private function myResult(e:ResultEvent):void{
                                                      var myXML:XML=XML(e.result);
                                                      myXMLCollection=new XMLListCollection(myXML..Customers);
                                                 }
                                  
                                            ]]>
                                       </mx:Script>
                                       <mx:HTTPService id="myService" url="GetDataTableXML.xml" result="myResult(event)" resultFormat="e4x" />
                                       <mx:DataGrid dataProvider="{myXMLCollection}">
                                            <mx:columns>
                                                 <mx:DataGridColumn dataField="CustomerID"></mx:DataGridColumn>
                                                 <mx:DataGridColumn dataField="CompanyName"></mx:DataGridColumn>
                                                 <mx:DataGridColumn dataField="ContactName"></mx:DataGridColumn>
                                                 <mx:DataGridColumn dataField="ContactTitle"></mx:DataGridColumn>
                                                 <mx:DataGridColumn dataField="Address"></mx:DataGridColumn>
                                                 <mx:DataGridColumn dataField="City"></mx:DataGridColumn>
                                                 <mx:DataGridColumn dataField="PostalCode"></mx:DataGridColumn>
                                                 <mx:DataGridColumn dataField="Country"></mx:DataGridColumn>
                                                 <mx:DataGridColumn dataField="Phone"></mx:DataGridColumn>
                                                 <mx:DataGridColumn dataField="Fax"></mx:DataGridColumn>
                                            </mx:columns>
                                       </mx:DataGrid>
                                  </mx:WindowedApplication>
                                  

                                  Just replace the URL, for your URL service. By the way this is an AIR app so change the WindowedApplication to just Application if you copy and paste this code.

                                  • 15. Re: .Net WebService to fill a dataProvider
                                    ntsiii Level 3

                                    Some suggestions:

                                    • Test your HTTPService url using a browser, make sure you have the xml response you expect.
                                    • Make sure you have set resultFormat="e4x"
                                    • Use a result handler like Michael shows, cast/convert event.result as XML, and trace(myXml.toXMLString()) to make sure you have the xml you expect.

                                     

                                    ARE you using HTTPService or are you using WebService?  Depinding on your .net app, your xml may have the SOAP wrapper stuff in it.  trace() to find out.

                                     

                                    Tracy