Expand my Community achievements bar.

Return a Single Record from HTTPService or Web Service?

Avatar

Level 1

I have managed to pull my data from the database and return it as XML.  However, if the data is a single record, it will not populate the ArrayCollection variable.. it returns on the Object.  (Which I'm sure most of you know.)

So what is the simplest way to display the record in a datagrid (or for that matter onto a form) if there is only that one record?

Seems it should not be too difficult.  After all, it's only one record. 

5 Replies

Avatar

Level 1

The ArrayCollection object will be populated only when you will have more then one object in your recordset. Now for displaying one record in data grid you first need to count the length of populated ArrayCollection object if it contain more then one record then simply say datagrid.dataprovider = ArrayCollection object and if the length of ArrayCollection is 0 or object in null then take your database reord in an object say myObj : Object = database record and then set datagrid.dataprovider = myObj

Hope this is clear to you.

Avatar

Level 1

Well, here's what I tried.. and it doesn't seem to be working.. it shows that "bratData" is "null"...

<mx:Script>
     <![CDATA[
          import mx.rpc.events.ResultEvent;
          import mx.collections.ArrayCollection;
         
          [Bindable]
          public var bratData:Object;
         
          public function resultHandler(event:ResultEvent):void {
              
               bratData = event.result.brats.brat;
          }
     ]]>
</mx:Script>

     <mx:WebService id="srv" wsdl="http://blah.blah.com/mycfc/db_getBrat.cfc?wsdl" useProxy="false" showBusyCursor="true">
          <mx:operation name="getaBrat" resultFormat="xml" result="resultHandler(event)">
               <mx:request>
                    <contactID>
                         {12345}
                    </contactID>
               </mx:request>
          </mx:operation>
                   
     </mx:WebService>

    <mx:DataGrid dataProvider="{bratData}" width="726" height="194" horizontalCenter="0" top="10">
        <mx:columns>
            <mx:DataGridColumn dataField="contactid" headerText="Contact ID" />
            <mx:DataGridColumn dataField="firstname" headerText="First Name"/>
            <mx:DataGridColumn dataField="lastname" headerText="Last Name"/>
        </mx:columns>
    </mx:DataGrid>
   
    <mx:Button label="Get Data" click="srv.getaBrat()" x="25" y="212"/>

However, when I test the webservice using a standard call from a coldfusion page, I do get data in XML format.. here's what I get:

xml document
XmlComment
XmlRoot
xml element
XmlNamebrats
XmlNsPrefix
XmlNsURI
XmlText
XmlComment
XmlAttributes
struct [empty]
XmlChildren
xml element
XmlNamebrat
XmlNsPrefix
XmlNsURI
XmlText
XmlComment
XmlAttributes
struct [empty]
XmlChildren
xml element
XmlNamecontactid
XmlNsPrefix
XmlNsURI
XmlText12345
XmlComment
XmlAttributes
struct [empty]
XmlChildren
xml element
XmlNamefirstname
XmlNsPrefix
XmlNsURI
XmlTextJody
XmlComment
XmlAttributes
struct [empty]
XmlChildren
xml element
XmlNamelastname
XmlNsPrefix
XmlNsURI
XmlTextCornelius
XmlComment
XmlAttributes
struct [empty]
XmlChildren
brats
XmlText
brat
XmlText
contactid
XmlText12345
firstname
XmlTextBob
lastname
XmlTextSmith

So my webservice IS returning an XML record.

Any suggestions?

Avatar

Level 1

Hi LeeMahn,

I have been looking for a solution to that for hours yesterday. And, then I came across this post:

http://polygeek.com/305_flex_what-happens-when-an-arraycollection-isnt-an-array#comment-30779

(see comment #3 there)

It did help me. Hope, you'll find that useful too There are a few typos in that code example, but I think it is easy to figure out how it works.

Avatar

Level 1

Thanks.. I'll look at that.

Take care...

Avatar

Level 1

Hey leemahn,

did u get any way to fix it?

if so plese do post the solution!

Thanks

Bhavika