Select to view content in your preferred language

How to test for empty results returned by query (URL)

2713
9
01-25-2011 05:38 AM
WillHughes1
Frequent Contributor
The data source is coldfusion.
The .mxml file passes a parameter to a query as a URL string and hits a coldfusion service and then displays the results on the page.
I want to add a conditional statement which checks to see if the query returns any data or no data.
I tried to get some help on this on the Adobe forums but no one could give me a solution.

As I understand it the code below should check to see if the value for CustomDatatype.FLOW==0. FLOW is one of the fields in the coldfusion data service. The default value for FLOW, when no data is returned, appears to be zero. So when no data is returned for FLOW the value is zero, and the Alert message that shows is "no data". This actually works if I don't put a query parameter in the URL, the "no data" message displays.
ex. http://localhost:8500/FireFlow2-debug/FireFlow2.html
However, if my URL is something like http://localhost:8500//FireFlow2-debug/FireFlow2.cfm?PRESSID=00349 , where there is definitely data for 00349, the "no data" message still displays, even when the FLOW value is 1021 - I can see the result in the form/datagrid.
So why is this condition still testing as FLOW==0? Any help is much appreciated. There probably is a better way to test for results than testing for a specific value for one of the fields.
Thank you.

-Will

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
      xmlns:s="library://ns.adobe.com/flex/spark"
      xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:flowtestservice1="services.flowtestservice1.*" xmlns:valueObjects="valueObjects.*"
    initialize="init()" width="1066" pageTitle="City of Durham - Fire Hydrant Flow Test Card">
<fx:Style source="FireFlow2.css"/>
<fx:Script>
  <![CDATA[
   import mx.collections.ArrayCollection;
   import mx.controls.Alert;
   import mx.core.mx_internal;
   import mx.events.EventListenerRequest;
   
   private var PRESSID:String = "0";
  
   private function init(): void
   {
     PRESSID = this.parameters.PRESSID;
     getFLOWTESTResult.token = fLOWTESTService1.getFLOWTEST(PRESSID);
    
     if (customDatatype.FLOW==0) {
      Alert.show ("no data");
     }else {
      Alert.show("There is some data!!!!!!!!!!!!!!");
    }
   }
   protected function button_clickHandler(event:MouseEvent):void
   {
    //if (fLOWTESTService1 != null) {
    //Alert.show("No flow test data for this fire hydrant.");
    //}else {
    getFLOWTESTResult.token = fLOWTESTService1.getFLOWTEST(pRESSIDTextInput.text);
    //}
   }
  
   private function dateFormat(item:Object, column:DataGridColumn):String{
    return dateFormatter.format(item [column.dataField]);
   }
  
  ]]>
</fx:Script>
<fx:Declarations>
  <flowtestservice1:FLOWTESTService1 id="fLOWTESTService1" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor="true"/>
  <s:CallResponder id="getFLOWTESTResult" result="customDatatype = getFLOWTESTResult.lastResult[0] as CustomDatatype"/>
  <valueObjects:CustomDatatype id="customDatatype"/>
  <mx:DateFormatter id="dateFormatter"
   formatString="MM-DD-YYY"/>
  <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:Form y="46" defaultButton="{button}" borderAlpha="0.5" cornerRadius="0" dropShadowVisible="true" borderColor="#A24E4E" x="258">
  <mx:FormItem label="Hydrant ID:">
   <s:TextInput id="pRESSIDTextInput"/>
  </mx:FormItem>
  <s:Button label="Get FLOW TEST" id="button" click="button_clickHandler(event)"/>
  <mx:Form borderAlpha="0.5">
   <mx:FormHeading label="Results:"/>
   <mx:FormItem label="Hydrant ID:">
    <s:Label id="pRESSIDLabel" text="{customDatatype.PRESSID}"/>
   </mx:FormItem>
   <mx:FormItem label="Date:">
    <mx:DateField enabled="false" id="dATEDateField" selectedDate="{customDatatype.DATE}"/>
   </mx:FormItem>
   <mx:FormItem label="Time:" contentBackgroundAlpha="1.0">
    <s:Label id="tIMELabel" text="{customDatatype.TIME}"/>
   </mx:FormItem>
   <mx:FormItem label="Location 1:">
    <s:Label id="lOC1Label" text="{customDatatype.LOC1}"/>
   </mx:FormItem>
   <mx:FormItem label="Location 2:">
    <s:Label id="lOC2Label" text="{customDatatype.LOC2}"/>
   </mx:FormItem>
   <mx:FormItem label="Flow (gpm):">
    <s:Label id="fLOWLabel" text="{customDatatype.FLOW}"/>
   </mx:FormItem>
   <mx:FormItem label="Static (psi):">
    <s:Label id="sTATICLabel" text="{customDatatype.STATIC}"/>
   </mx:FormItem>
   <mx:FormItem label="Residual (psi):">
    <s:Label id="rESIDUALLabel" text="{customDatatype.RESIDUAL}"/>
   </mx:FormItem>
   <mx:FormItem label="PRESLOC">
    <s:Label id="pRESLOCLabel" text="{customDatatype.PRESLOC}"/>
   </mx:FormItem>
   <mx:FormItem label="Q20 (gpm):">
    <s:Label id="q20Label" text="{customDatatype.Q20}"/>
   </mx:FormItem>
   <mx:FormItem label="FLOWID">
    <s:Label id="fLOWIDLabel" text="{customDatatype.FLOWID}"/>
   </mx:FormItem>
   <mx:FormItem label="FLOWLOC">
    <s:Label id="fLOWLOCLabel" text="{customDatatype.FLOWLOC}"/>
   </mx:FormItem>
  
  </mx:Form>
</mx:Form>
<mx:DataGrid x="72" y="549" id="dataGrid" dataProvider="{getFLOWTESTResult.lastResult}" width="904" height="160" editable="false" variableRowHeight="true">
  <mx:columns>
   <mx:DataGridColumn headerText="PRESSID" dataField="PRESSID" wordWrap="false"/>
   <mx:DataGridColumn headerText="Date" dataField="DATE" labelFunction="dateFormat"/>
   <mx:DataGridColumn headerText="TIME" dataField="TIME"/>
   <mx:DataGridColumn headerText="LOC1" dataField="LOC1" wordWrap="true"/>
   <mx:DataGridColumn headerText="LOC2" dataField="LOC2" wordWrap="true"/>
   <mx:DataGridColumn headerText="FLOW" dataField="FLOW"/>
   <mx:DataGridColumn headerText="STATIC" dataField="STATIC"/>
   <mx:DataGridColumn headerText="RESIDUAL" dataField="RESIDUAL"/>
   <mx:DataGridColumn headerText="PRESLOC" dataField="PRESLOC" wordWrap="true"/>
   <mx:DataGridColumn headerText="Q20" dataField="Q20"/>
   <mx:DataGridColumn headerText="FLOWID" dataField="FLOWID"/>
   <mx:DataGridColumn headerText="FLOWLOC" dataField="FLOWLOC" wordWrap="true"/>
  </mx:columns>
</mx:DataGrid>
<mx:ColumnChart x="594" y="117" id="columnchart1" dataProvider="{getFLOWTESTResult.lastResult}" showDataTips="true" width="431" height="373">
  <mx:series>
   <mx:ColumnSeries displayName="Flow" yField="FLOW" id="columnSeries"/>
   <mx:ColumnSeries id="columnSeries4" displayName="Q20" yField="Q20"/>
  </mx:series>
  <mx:horizontalAxis>
   <mx:CategoryAxis id="categoryAxis" categoryField="DATE"/>
  </mx:horizontalAxis>
</mx:ColumnChart>
<mx:Legend dataProvider="{columnchart1}"/>
<mx:Image x="103" y="96" source="http://localhost:8500/FireFlow2-debug/durhamlogo.PNG"/>
<s:RichText x="72" y="261" text="*This information represents flows and pressures at one specefic time. These parameters constantly fluctuate. Designers should use appropriate factors of sfety when using this information for design purposes." width="151" height="162" verticalAlign="middle" textAlign="center"/>
<s:RichText x="171" y="11" text="Fire Hydrant Flow Test Card" fontSize="24" verticalAlign="middle" textAlign="center" fontWeight="bold"/>
<s:Label x="72" y="529" text="Flow Test History:" fontWeight="bold" fontSize="14"/>
</s:Application>
Tags (2)
0 Kudos
9 Replies
RobertScheitlin__GISP
MVP Emeritus
Will,

   I have seen this happen many times. It is like flash builder has the var stuck as some result that is actually not that value anymore. Try a couple of things one is to run a "clean" from the project menu. If that does not do it then just shut down builder and restart then do a clean again.
0 Kudos
WillHughes1
Frequent Contributor
I cleaned the project, closed and reopened FB4, cleaned again and still no luck.

Is there a simpler way to accomplish this, such as to check if the datagrid is empty?

-Will
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Will,

   Maybe check the rowCount of the datagrid.
0 Kudos
WillHughes1
Frequent Contributor
I tried to use dataGrid.rowCount but it is not counting the number of rows for some reason.
It always returns zero as the count even when there is at least 1 row in the datagrid.

This makes me think that getFLOWTESTResult.lastResult is always empty at this point in the code, but I am not sure where else I can place it.
At least I know that it is returning nothing (zero) rows at this point.

-Will

private function init(): void
   {
     PRESSID = this.parameters.PRESSID;
     getFLOWTESTResult.token = fLOWTESTService1.getFLOWTEST(PRESSID);
     dataGrid.rowCount=getFLOWTESTResult.lastResult;
    
     if (dataGrid.rowCount>=1) {
      Alert.show("No Data");
     }else {
      Alert.show (dataGrid.rowCount + " is the count");
    }
   }
0 Kudos
by Anonymous User
Not applicable
Remote service calls made by flex are asyncronous, so maybe the request hasn't completed yet. Have you tried listening to the callresponders result event?
0 Kudos
WillHughes1
Frequent Contributor
Michael,

Do you have an example for how to listen to the callresponders result event?
Thanks.

-Will
0 Kudos
WillHughes1
Frequent Contributor
We'll I got it to work using a completionCreateHandler.
Basically where the datagrid is complete, it calls the dataGrid_creationCompleteHandler function which checks the row count for the datagrid.

I also had to change the dataGrid.rowCount to = Array(getFLOWTESTResult.lastResult).length;
, which gives me the following warning... but it still works.
Description Resource Path Location Type
1112: Array(x) behaves the same as new Array(x).  To cast a value to type Array use the expression x as Array instead of Array(x). FireFlow2.mxml /FireFlow2/src line 26 Flex Problem

-Will

<fx:Script>
  <![CDATA[
   import mx.collections.ArrayCollection;
   import mx.controls.Alert;
   import mx.controls.DataGrid;
   import mx.core.mx_internal;
   import mx.events.EventListenerRequest;
   import mx.events.FlexEvent;
   import mx.rpc.CallResponder;
   import mx.rpc.events.ResultEvent;
 
  
   private var PRESSID:String = "";
  
  
   private function init(): void  
   {
     PRESSID = this.parameters.PRESSID;
     getFLOWTESTResult.token = fLOWTESTService1.getFLOWTEST(PRESSID);
     dataGrid.rowCount=Array(getFLOWTESTResult.lastResult).length;
   }
   protected function button_clickHandler(event:MouseEvent):void
   {
    //if (fLOWTESTService1 != null) {
    //Alert.show("No flow test data for this fire hydrant.");
    //}else {
   
    if (dataGrid.rowCount>1) {
    getFLOWTESTResult.token = fLOWTESTService1.getFLOWTEST(pRESSIDTextInput.text);
   
     Alert.show (dataGrid.rowCount + " is the count .... there is data.");
    }else {
     Alert.show("No Data");
    }
   }
  
   private function dateFormat(item:Object, column:DataGridColumn):String{
    return dateFormatter.format(item [column.dataField]);
   }


   protected function dataGrid_creationCompleteHandler(event:FlexEvent):void
   {
    if (dataGrid.rowCount<=1) {
     Alert.show("No Data ....... and the row count is... " + dataGrid.rowCount);
    }else {
     Alert.show ("There is some data " + dataGrid.rowCount + " is the counTTTTTTTT");
    }
   }
  ]]>
</fx:Script>
0 Kudos
by Anonymous User
Not applicable
The only problem I see with that is that the result from the call responder still may not have returned a result by the time the creation event is fired. You can handle the result event like you do the fault event where event.result contains the result of the call:

<s:CallResponder id="myResponder" result="myResponder_resultHandler(event)" ...

//as
protected function myResponder_resultHandler(event:ResultEvent):void
{
          // TODO Auto-generated method stub
}
0 Kudos
WillHughes1
Frequent Contributor
I see. I'll give that a try. Thanks.

-Will
0 Kudos