2 Replies Latest reply on Feb 8, 2010 8:55 AM by Rymasek

    Why HTTPService is throwing error when there is two or less rows of data ?!

    Rymasek

      Hi guys,

       

      I have an issue with HTTPService which is this.

      I'm using HTTPService to retrieve data from SQL database, and to display those data in datagrid.

      Basically MXML and script is like this :

       

      example code  ---------------------------------------------------------------------

       

      <mx:HTTPService id="readRequestC"

                                         method="POST"

                                         useProxy="false"

                                         url="http://www.mydomain.com/read_record.php"

                                         result="showActiveAlerts(event)">

      </mx:HTTPService>

       

       

      private function showActiveAlerts(e:ResultEvent):void

                                         {

                                         C_activealerts_AC = e.result.patientalert.alert                         

                                         }

      end of code ------------------------------------------------------------------------

       

      C_activealerts_AC - array collection is bound to data grid

       

       

      I'm using this read_record.php file to retrieve data from database :

       

      example code  ---------------------------------------------------------------------

      <?php

      define( "DATABASE_SERVER", "xxx" );

      define( "DATABASE_USERNAME", "xxx" );

      define( "DATABASE_PASSWORD", "xxx" );

      define( "DATABASE_NAME", "xxx" );

       

      //connect to the database.

      $mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD);

       

      mysql_select_db( DATABASE_NAME );

       

      //--------------------------------------------------------------------------------

      $Query = "SELECT * FROM patientalert";

      $Result = mysql_query( $Query );

       

      // ----------------------------------- xml is constructed here -------------------

      $Return = "<patientalert>";

                  while ( $User = mysql_fetch_object( $Result ) )  

                  {          

                  $Return .= "<alert>

                                                     <idstaff>".$User->idstaff."</idstaff>

                                                     <idpatient>".$User->idpatient."</idpatient>

                                                     <idalerttype>".$User->idalerttype."</idalerttype>

                                                     <idalertstatus>".$User->idalertstatus."</idalertstatus>

                                                      <timestampappear>".$User->timestampappear."</timestampappear>

                                                     <timestampproceed>".$User->timestampproceed."</timestampproceed>

                                                     <position>".$User->position."</position>

                                                     <mobileid>".$User->mobileid."</mobileid>

                                                     </alert>";

                  }          

      $Return .= "</patientalert>";

      // --------------------------------------------------------------------------------

      mysql_free_result( $Result );

      print ($Return)

       

      ?>

      end of code ------------------------------------------------------------------------

       

       

      This php file above is creating nice XML file which I'm reading with HTTPService. Everything is working fine when there is a lot of data (at least several rows)

       

      The point is, that data is changing constantly in SQL base, there can be 10 rows, but there can be 3 of them... and here is the problem.

      When there is less than 2 row of data... an error appears :

       

      TypeError: Error #1034: Can not convert type mx.utils::ObjectProxy@4011089 to type mx.collections.ArrayCollection....

       

       

      I'm sure this problem is due to fact that when there is only one row in database we don't have several <alert> nodes in XML so it cannot be converted into ArrayCollection.

       

      Do you have any idea how to prevent this ? How to convert one row of data from database to valid Array Collection... and what if there will be no data in the table... in that case I simply want to display no data in data grid !

       

      Thanks for any help !!!

       

       

        • 1. Re: Why HTTPService is throwing error when there is two or less rows of data ?!
          Matt Le Fevre Level 4

          When there is only a single row being returing, it isn't an Array collection, effectively it's just returning a single object.

           

           

           

          Im imagine you're feeding the HTTPService lastResult into an array collection, and then binding that to the Datagrid.

           

           

          I'd suggest capturing the result of the HTTP service and then feeding that into the Datagrid.

           

          something along the lines of:

           

          <mx:HTTPService id="readRequestC"
                                             method="POST"
                                             useProxy="false"
                                             result="handleRequestC(event)"
                                             url="http://www.mydomain.com/read_record.php"
                                             result="showActiveAlerts(event)">
          </mx:HTTPService>
          
          
          private function handleRequestC(evt:ResultEvent):void
          {
               Datagrid.dataprovider = evt.result.patientalert.alert;
          }
          

           

          <mx:DataGrid>
                  <mx:columns>
                      <mx:DataGridColumn dataField="idstaff"/>
                      <mx:DataGridColumn dataField="idpatient"/>
                      <mx:DataGridColumn dataField="idalerttype"/>
                      etc...
                  </mx:columns>
          </mx:DataGrid>
          

           

           

           

          you'll also need

           

          import mx.rpc.events.ResultEvent;
          
          • 2. Re: Why HTTPService is throwing error when there is two or less rows of data ?!
            Rymasek Level 1

            >>>> Im imagine you're feeding the HTTPService lastResult into an array collection, and then binding that to the Datagrid.

             

            yes indeed... I'm doing precisely that :

             

            I've got an array collection

                        [Bindable]
                        private var C_activealerts_AC:ArrayCollection=new ArrayCollection()

             

            which is bound to datagrid :

             

            <mx:DataGrid dataProvider="{C_activealerts_AC}">
                         <mx:columns>
                                <mx:DataGridColumn headerText="Staff" dataField="idstaff"/>
                                <mx:DataGridColumn headerText="Patient" dataField="idpatient"/>
                                <mx:DataGridColumn headerText="Type" dataField="idalerttype"/>

                                <mx:DataGridColumn headerText="Status" dataField="idalertstatus"/> ...etc..

                         </mx:columns>
            </mx:DataGrid>

             

            and I'm pushing event.result to C_activealerts_AC, like this :

             

            <mx:HTTPService id="readRequestC"
                                method="POST"
                                useProxy="false"                   
                                url="http://www.mydomain.com/read_record.php"
                                result="showActiveAlerts(event)">       
            </mx:HTTPService>

             

            // -------------------------

             

            private function showActiveAlerts(e:ResultEvent):void {
                 C_activealerts_AC = e.result.patientalert.alert

            }

             


            Now... I'm looking at your solution but I'm not quite catching it ... you have

            two results in HTTPService ? Compiler doesn't like this it's shouting 'result has been defined once' !

            result="handleRequestC(event)"
            result="showActiveAlerts(event)"

             

            and what is this 'Datagrid' below... is it an id of my datagrid ?? hmm..

            Datagrid.dataprovider = evt.result.patientalert.alert;

             

            Could you please explain it a little more ?

            I thought if there is one row of data, array collection will be feeded only with one object... apparently, like you say, it's not

             

            Thanx