9 Replies Latest reply on Oct 27, 2010 3:38 AM by TheGarfunkal

    Response from server into arraycollection trauma

    TheGarfunkal Level 1

      Hi.

      Hopefully someone can help - coz i am freaking out, and can't find a solution.

       

      I am developing an application that takes a variety of bits of info and displays them in pods within a page.

       

      One of the bits of info takes ONE row from a database table and tries (currently) to put it into a datagrid.

      Now, I know Flex wont allow that, so I have tried all kinds of things to fix it - using the xml directly, trying to get the info into labels casting as an array, using xmlistcollections... so much time on Google but now I am stuck.  I'm not precious about getting the info into a datagrid - in fact I'd rather put it into labels or text boxes so I can presents it in a more flexible way. 

       

      Looking in the debugger, it seems like I don't even have the data in my variable - I do have it in the event so it is there, but I can't for the life of me get it out...

      This obviously works fine for multiple rows of data in the array - not the single row.  i'm trying to get the info into a var called _SystemStatus. I dont even know if that is the right way to go

      Please any suggestions?

       

      Overview of Code:

      I have a mxml but with all the presentation stuff in.  I also have two actionscript modules with code in (you may have  guessed that I am new to this, so I have found most of this great code on the internet)

       

      The AS bits are

      sql.as - this basically is a load of functions called by the mxml which contain the sql to send to the database

      mssql.as - this does all the magic of getting the data from the db, and taking it from the event.

       

       

      Current Debugger output:

       

       

      debug.JPG

       

       

      Code below:

       

      CompletionTimes.mxml (note - its got errors in where i have tried things)

       

       

      <?xml version="1.0" encoding="utf-8"?>

       

      <PodContentBase

       

      xmlns="dashboard.view.ActionScript.*"

      xmlns:mx="http://www.adobe.com/2006/mxml"

      width="100%" height="100%">

       

       

       

      <mx:Script source="ActionScript/MSsql.as"/>

      <mx:Script source="ActionScript/sql.as"/>

       

       

       

       

      <mx:Text text="This is the trend overview. Maximise Pod to view details."/>

          <mx:TextArea text="{CMIPortal.ChosenSystem}"/>

       

       

       

      <mx:Label text="'gfgfg'+ '{_SystemStatus.getItemAt(0)}'" />

       

      <mx:DataGrid  id="dgSystemStatus" height="86" width="100%" creationComplete="{SystemStatus()}" dataProvider="{_SystemStatus}" themeColor="#E166B5" color="#3C3C3C" >

      <!-- <mx:columns>

      <mx:DataGridColumn dataField="Application" headerText="Application"   />

      <mx:DataGridColumn dataField="Available" headerText="Available"  width="65"  />

      <mx:DataGridColumn dataField="Last_Update" headerText="Last Update"  color="#3C3C3C"  />

      <mx:DataGridColumn dataField="Data_Current" headerText="Data Current"/>

      </mx:columns> -->

      </mx:DataGrid>

       

       

       

       

       

      </PodContentBase>

       

       

      sql.AS

       

       

      // ActionScript file

       

      import mx.events.ListEvent;

      import mx.controls.Alert;

       

      private function SystemStatus():void {

       

         mssqlQuery("SELECT * FROM [MI_Specialists].[dbo].[Portal_SystemStatus] WHERE [Application] =  '" +  CMIPortal.ChosenSystem   + "' AND  ID in ( SELECT MAX([Id]) [Application] FROM  [MI_Specialists].[dbo].[Portal_SystemStatus] group by [Application]) ","SystemStatus");

         }

      }

      msSql.As
      // ActionScript file
      import mx.charts.*;
      import mx.collections.ArrayCollection;
      import mx.collections.XMLListCollection;
      import mx.controls.Alert;
      import mx.rpc.AsyncToken;
      import mx.rpc.events.FaultEvent;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.http.mxml.HTTPService;
      import mx.utils.ArrayUtil;
      private var sqlToken:AsyncToken;
      /********************************************************
      * Your Configuration Paramaters
      *
      *********************************************************/
      public var mssql_db:String = "MI_Specialists";
      /********************************************************
      * MS SQL Query Function
      *********************************************************/
      public function mssqlQuery(sql:String,fid:String):void {
      var http:HTTPService = new HTTPService;
      var parm:Object = new Object;
      parm.fas_sql = sql;
      parm.fas_db = mssql_db;
      http.url = mssql_url+"?irand="+Math.random();
      http.showBusyCursor = true;
      http.request = sql;
      http.addEventListener(ResultEvent.RESULT, mssqlResult);
      http.addEventListener(FaultEvent.FAULT, mssqlFault);
      http.method = "POST";
      http.resultFormat="e4x"
      sqlToken = http.send(parm);
      sqlToken.param = fid;
      }
      private function mssqlFault(event:FaultEvent):void {
      var err:String = event.fault.faultString;
      Alert.show(err);
      // fname.text = err
      }
      [Bindable]
      private  var _SystemStatus:ArrayCollection = new ArrayCollection();
      private function mssqlResult(event:ResultEvent):void{
      //Note: All Results are stored in the
      // event.result object
      //Create a new case/break for each of your
      //sql query statements
      switch(event.token.param){
      case "SystemStatus":
      _SystemStatus = new ArrayCollection([event.result.results.record]);
      // _SystemStatus =  event.result.results.record;
      break;
      break;
      }
      }
      /********************************************************
      * Functions: Query Requests
      *
      *********************************************************/
      public function addslashes(tt:String):String{
      //replaces all occurrences
          var ttt:String = tt.replace(/\u0027+/g,"''");
      return ttt;
      }
        • 1. Re: Response from server into arraycollection trauma
          Claudiu Ursica Level 4

          DO you get the data ater making the http call?

          C

          • 2. Re: Response from server into arraycollection trauma
            TheGarfunkal Level 1

            Hi Ursica

             

            I assume so, as the data is returned into the event.result.results.record - I can see it in the debugger.

            Maybe I need to think of a way to get the data from there into another control or collection. I've tried loads - no success.

            • 3. Re: Response from server into arraycollection trauma
              Claudiu Ursica Level 4

              I see you have XML response. I rarely bind to XML, instead I use a custom

              unmarshaller to parse data into come custom typed class. You can build also a

              wrapper class over the Xml to expose data through bindable getters/setter.

              Whatever suits you better.

               

              However I think XMLListCollection it seems more appropriate from what I see

              you've posted.

               

              Here I think you have the issues. event.result.results probably returns an

              XMLList. check to see if it has valid results. If so wrap it inside a

              XMLListCollection. new XMLListCollection(xmlLIst).

               

               

              _SystemStatus = your new listCollection.

               

              P.S.

              Try it on your own since I don't have your code to compile so I write from my

              memory.

               

              HTH,

              C

              • 4. Re: Response from server into arraycollection trauma
                TheGarfunkal Level 1

                Hi - thanks again fro your help.

                 

                I'm not sure how I check that the XMLlist returned valid results

                The event result says it is XML, and the Variables I've set up are shown in the debugger as:

                 

                _135458828_SystemStatusColl mx.collections.XMLListCollection (@3209281)

                _1667321088_SystemStatus XMLList (@32463a1)

                 

                So, I assume the _SystemStatus IS an XMLList -  However the debugger shows no data for it.

                 

                Ive changed the code in msSql.as to have an extra variable:

                 

                [Bindable]

                // private  var _SystemStatus:ArrayCollection = new ArrayCollection();

                private  var _SystemStatus:XMLList  = new XMLList();

                 

                [Bindable]

                 

                private  var _SystemStatusColl:XMLListCollection;

                 

                and changed the case to say

                 

                case "SystemStatus":

                 

                 

                // _SystemStatus = new ArrayCollection([event.result.results.record]);

                        _SystemStatus = event.result.results.record;

                       

                _SystemStatusColl =  new XMLListCollection(_SystemStatus);

                 

                break;

                Still no data in the grid!  Any suggestions?

                • 5. Re: Response from server into arraycollection trauma
                  Claudiu Ursica Level 4

                  Send me an sample XML and I;ll parse it for you I don't have time to create it

                  by hand from the image but you have the debugger so just do a copy paste.

                  C

                  • 6. Re: Response from server into arraycollection trauma
                    TheGarfunkal Level 1

                    The data  copied from the record level of the event.result.results.records debugger is:

                     

                    <record>

                      <Id>

                        <![CDATA[54]]>

                      </Id>

                      <Todays_Date>

                        <![CDATA[14/09/2010]]>

                      </Todays_Date>

                      <Application>

                        <![CDATA[AIS MI]]>

                      </Application>

                      <Status>

                        <![CDATA[Final]]>

                      </Status>

                      <Available>

                        <![CDATA[Yes]]>

                      </Available>

                      <Last_Update>

                        <![CDATA[14/09/2010]]>

                      </Last_Update>

                      <Completion_Time>

                        <![CDATA[10.49]]>

                      </Completion_Time>

                      <Data_Current>

                        <![CDATA[13/09/2010]]>

                      </Data_Current>

                      <Comments>

                        <![CDATA[]]>

                      </Comments>

                    </record>

                     

                     

                    event mx.rpc.events.ResultEvent (@2f8eee1)

                    [inherited]

                    headers null

                    result XML

                    <results>

                    <record>

                    <Id>

                    <Todays_Date>

                    <Application>

                    <Status>

                    <Available>

                    <Last_Update>

                    <Completion_Time>

                    <Data_Current>

                    <Comments>

                    statusCode 200 [0xc8]

                    • 7. Re: Response from server into arraycollection trauma
                      Claudiu Ursica Level 4

                      I created a custom class it is more straight forward and you can debug...

                       

                      package

                      {

                      public class Record

                      {

                      public var id:uint;

                      public var todaysDate:String; // put some date from string conversion here and

                      type as Date if necessary;

                      public var application:String;

                      public var status:String;

                      public var available:Boolean;

                      public var lastUpdate:String;

                      public var completionTime:Number;

                      public var dataCurrent:String;

                      public var comments:String;

                      public function toString():String

                      {

                      return "Record id = " + id +

                      " todaysDate = " + todaysDate +

                      " application = " + application +

                      " status = " + status +

                      " available = " + available +

                      " lastUpdate = " + lastUpdate +

                      " completionTime = " + completionTime +

                      " dataCurrent = " + dataCurrent +

                      " comments = " + comments;

                      }

                      }

                      }

                       

                       

                      hardcoded XML

                       

                      private var resultXML:XML = ;

                      parsing code

                       

                       

                      var record:Record = new Record();

                      var result:XMLList = resultXML.record;

                      record.id = result.Id;

                      record.todaysDate = result.Todays_Date; // put some date from string conversion

                      here and type as Date if necessary;

                      record.application = result.Application;

                      record.status = result.Status;

                      record.available = result.Available;

                      record.lastUpdate = result.Last_Update; // put some date from string conversion

                      here and type as Date if necessary;

                      record.completionTime = result.Completion_Time;

                      record.dataCurrent = result.Data_Current; // put some date from string

                      conversion here and type as Date if necessary;

                      record.comments = result.Comments;

                      trace(record);

                       

                       

                      I don;t have time for the XML wrapper version now sorry. This should take u

                      forward. What you have to do is something like resultXML = event.result as XML;

                      instead of the hardocding part

                      C

                      • 8. Re: Response from server into arraycollection trauma
                        TheGarfunkal Level 1

                        I was having a mess and found that i can reference the event.result directly....

                         

                        [Bindable]

                        private  var _SSApplication:String = new String;

                        _SSApplication = event.result.*[0].*[2]; 

                         

                        As i know there is only going to be one result that seems to suffice.  Phew!

                         

                        Claudiu - thanks so much for your help - does my way look ok? I found it by messing in the debugger - using watch expressions.

                        • 9. Re: Response from server into arraycollection trauma
                          Claudiu Ursica Level 4

                          Check if the reference to the event is released after the assigned. If so you

                          are good to go ...

                          C