4 Replies Latest reply on Jan 15, 2008 7:05 PM by chris.huston.t10

    Combobox populate from HTTPService query

      I got this working with static XML files, but now its not working with HTTPService! I'm having problems getting a ComboBox to fill up with dynamic data from an SQL query which spits back XML data. I am basically filling one ComboBox based on the selection from another ComboBox. On startup, I call this method:

      public function loadSystems(serv:HTTPService, s:String) : void {
      var params:Object = {};
      params[s] = s;

      To fill in the first ComboBox, which is defined as:

      <mx:ComboBox minWidth="130" maxWidth="130" id="mainSys" dataProvider="{sysOps.lastResult.system.data}"
      change="fillComboBox(mainSys, subSys, 'subsystem', subOps, sysIDs)" rowCount="10" />

      It fills up fine, but the fillComboBox method (below) comes up with a null result and fails

      // fill destination combobox based on selection from src combobox
      public function fillComboBox(src:ComboBox, dest:ComboBox, arg:String,
      serv:HTTPService, idArray:ArrayCollection) : void {
      var ind:int = src.selectedIndex;
      // choose ID from the ID array with the selecetedIndex
      var select:String = idArray.getItemAt(ind) as String;
      var params:Object = {};
      params[select] = select;
      params[arg] = arg;


      dest.dataProvider = serv.lastResult.system.data; // serv.lastResult is null

      I know the server side is getting valid arguments in params and is spitting out valid XML so the problem is on the Flex side. Any ideas or help would be greatly appreciated. Thanks!
        • 1. Re: Combobox populate from HTTPService query
          atta707 Level 2
          1) try adding fault handle on your http service if you haven't already and see if something is failing.
          2) try tricking browser cache by adding a dummy time stamp to the parameters:

          var params:Object = new Object();
          params.select = select;
          params.arg = arg;
          params.dummy = new Date();


          • 2. Re: Combobox populate from HTTPService query
            chris.huston.t10 Level 3
            Although caching is a common problem, that is not the cause of your problem. In Flex, all HTTPService calls are asynchronous, which means you cannot call the service and set the result in the same function like you are doing in your fillComboBox method. You need to add an event listener and get the result in a different method. However, in your function you are also passing the destination which complicates things. Instead of passing the destination, you could try passing a function that will assign the result to the component you want dynamically:

            change="fillComboBox(mainSys, setSubSys, 'subsystem', subOps, sysIDs)"
            public function fillComboBox(src:ComboBox, resultFunction:Function, arg:String, serv:HTTPService, idArray:ArrayCollection) : void

            private function setSubSys(evt:ResultEvent):void {
            subSys.dataProvider = evt.result.system.data;

            In general, it makes debugging much easier if you avoid binding HTTPService results directly to components and instead use a listener function where you can examine the result data for any problems.

            • 3. Re: Combobox populate from HTTPService query
              Solerous Level 1
              Thanks for the replies. I had seen the eventlistener method of capturing httpservice results before, but I did not know that the calls were asynchronous and so this makes more sense now. However, I came up with an even simpler hack which seems to work. I just assigned the dataprovider prematurely:

              <mx:ComboBox minWidth="130" maxWidth="130" id="mainSys" dataProvider="{sysOps.lastResult.system.data}"
              change="fillComboBox(mainSys, subSys, 'subsystem', subOps, sysIDs, 1)" rowCount="10" />
              <mx:ComboBox minWidth="130" maxWidth="130" id="subSys" dataProvider="{subOps.lastResult.system.data}"
              change="fillComboBox(subSys, device, 'devices', devOps, subIDs, 2)"/>

              So even though subOps doesn't get called until later, it seems to work to assign it in the ComboBox definition. Does this seem like a good fix? As I said, its working, but if there are problems I'm not seeing with this solution, let me know.
              • 4. Re: Combobox populate from HTTPService query
                chris.huston.t10 Level 3
                Your solution will work, but if you ever need to debug, it will be quite difficult. By using a result listener method, you will be able to look at the returned value and check for any problems. If you bind directly to the lastResult, you will not be able to debug the result at all. For simple projects, this might not be an issue, but for larger projects, it can be a concern. I would advise using the event listener approach in all cases. It does not require much extra code and can save a lot of time in the future.