Sample code using HTTPService, e4x, handler function to populate a list item

The DataGrid tag:
<mx:DataGrid id="dg" dataProvider="{_xlcMyListData}" .../>


The HTTPService tag:
<mx:HTTPService id="service" resultFormat="e4x" result="onResult(event)" 
fault="..../>

Script block declaration:
import mx.rpc.Events.ResultEvent;
[Bindable]private var _xlcMyListData:XMLListCollection;

Result Handler function:
private function onResult(oEvent:ResultEvent):void  {
  var xmlResult:XML = XML(event.result); //converts result Object to XML. can 
also use "as" operator
  var xlMyListData:XMLList = xmlResult.myListData; //depends on xml format, is 
row data
  _xlcMyListData = new XMLListCollection(xlMyListData); //wrap the XMLList in a 
collection
  trace(_xlcMyListData.toXMLString());  //so you can see exactly how to specify 
dataField or build labelFunction

________________________________________
From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of Tracy 
Spratt
Sent: Friday, March 30, 2007 1:35 PM
To: [email protected]
Subject: RE: [flexcoders] Issues with Datagrid non repeatinh HTTPService XML 
Result.

I think you will be happy with HTTPService.
 
Specify resultFormat="e4x" in the HTTPService tag, otherwise flex converts the 
xml into an nested mx:Object structure.  I find e4x XML much easier to work 
with.
 
Don't bind directly to lastResult. It is too hard to debug. Instead use a 
result handler, and set an instance variable value(XMLListCollection) to 
event.result.STAFFREC in that, and bind the DataGrid dp to the instance 
variable.  In the handler, you can trace the instance variable.toXMLString() to 
see exactly what yo have, and how to set the DataGridColumn dataField or 
labelFunction.
 
I've written sample code tor this so many times I don't want to do it again 
right now, but post if you need more detail.
 
Tracy 
 
________________________________________
From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of Olonade 
Tolulope williams
Sent: Friday, March 30, 2007 6:17 AM
To: [email protected]
Subject: [flexcoders] Issues with Datagrid non repeatinh HTTPService XML Result.
 
I have a flex DBGrid client that was supposed to consume a web 
service client. After much tweaking and coding. No success. I 
switched to HTTPService and this was returning xml results.
But when DBGrid accepts the results they are all listed ONLY in row 1 
of the DBGrid.
Below is the mxml application as well as the web service I 1st used 
and finally the httpservice routine done in C# 2.0
Any help will be appreciated.
Thanks.

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; 
layout="absolute">
<mx:HTTPService id="SDSearch" 
url="http://localhost:53541/SDSearch.NET/Default.aspx"; 
showBusyCursor="true">
<mx:request>
<byName>{NameBox.text}</byName>
<byPhoneNum>{PhoneBox.text}</byPhoneNum>
</mx:request>

</mx:HTTPService>
<mx:DataGrid enabled="true" id="dataGrid" editable="false" 
x="130" y="179" width="411" height="273" 
dataProvider="{SDSearch.lastResult.STAFFREC}" 
alternatingItemColors="[#fcf7e4, #ffffff]">
<mx:columns>

<mx:DataGridColumn headerText="Staff Name" 
dataField="STAFFNAME"/>
<mx:DataGridColumn headerText="Phone Number" 
dataField="PHONE_NUMBER"/>

</mx:columns>
</mx:DataGrid>
<mx:Label x="28" y="88" text="Enter Name:"/>
<mx:Label x="28" y="131" text="Enter Phone No:"/>
<mx:TextInput id="NameBox" x="144" y="86" width="382"/>
<mx:TextInput id="PhoneBox" x="144" y="129" width="382"/>
<mx:Button x="247" y="530" label="Invoke Service" 
click="SDSearch.send();"/> 

</mx:Application>

public class Employee
{
public string STAFFNAME;
public string Phone_Num;
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string byName, byPhoneNum;
string queryString, cnString;
OleDbDataAdapter adapter;
DataSet EmpData = new DataSet();
cnString = ConfigurationManager.AppSettings
["ConnStrDB"].ToString();

byName = Request.Params["byName"].ToString().Trim();
byPhoneNum = Request.Params["byPhoneNum"].ToString().Trim
();

if (byPhoneNum.Length == 11)
{
queryString = "SELECT * FROM Employees WHERE 
PHONE_NUMBER = '" + byPhoneNum + "'";
adapter = new OleDbDataAdapter(queryString, cnString);
adapter.Fill(EmpData, "Employees");

Response.Write("<STAFFREC>");
foreach (DataRow currrec in EmpData.Tables[0].Rows)
{
Response.Write("<STAFFNAME>" + currrec
["STAFFNAME"].ToString() + "</STAFFNAME>");
Response.Write("<PHONE_NUMBER>" + currrec
["PHONE_NUMBER"].ToString() + "</PHONE_NUMBER>");
}
Response.Write("</STAFFREC>");
//XmlDataDocument dataDoc = new XmlDataDocument
(EmpData);
//return dataDoc;
}
else 
{
queryString = "SELECT * FROM Employees WHERE 
STAFFNAME LIKE '%" + byName + "%'";
adapter = new OleDbDataAdapter(queryString, cnString);
adapter.Fill(EmpData, "Employees");

Response.Write("<STAFFREC>");
foreach (DataRow currrec in EmpData.Tables[0].Rows)
{
Response.Write("<STAFFNAME>" + currrec
["STAFFNAME"].ToString() + "</STAFFNAME>");
Response.Write("<PHONE_NUMBER>" + currrec
["PHONE_NUMBER"].ToString() + "</PHONE_NUMBER>");
}
Response.Write("</STAFFREC>");

//XmlDataDocument dataDoc = new XmlDataDocument
(EmpData);
//return dataDoc;
}
}
}

// the web service..
[WebService(Namespace = "http://tempuri.org/";)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Employee
{
public string STAFFNAME;
public string Phone_Num;
}
public class SDSearch : System.Web.Services.WebService
{
public SDSearch () {

//Uncomment the following line if using designed components 
//InitializeComponent(); 
}

[WebMethod]
public XmlDataDocument getStaffMatch(string byName, string 
byPhoneNum)
{
string queryString,cnString;
OleDbDataAdapter adapter;
DataSet EmpData = new DataSet();
cnString = ConfigurationManager.AppSettings
["ConnStrDB"].ToString();

if (byPhoneNum.Length == 11)
{
queryString = "SELECT * FROM Employees WHERE PHONE_NUMBER 
= '" + byPhoneNum + "'";
adapter = new OleDbDataAdapter(queryString, cnString);
adapter.Fill(EmpData,"Employees");
XmlDataDocument dataDoc = new XmlDataDocument(EmpData);
return dataDoc;
}
else //if (byName.Length > 3)
{
queryString = "SELECT * FROM Employees WHERE STAFFNAME 
LIKE '%" + byName + "%'";
adapter = new OleDbDataAdapter(queryString, cnString);
adapter.Fill(EmpData, "Employees");
XmlDataDocument dataDoc = new XmlDataDocument(EmpData);
return dataDoc;
}

}

}
 

Reply via email to