[Flex 4 and .Net] Retrieving tables from SQL database
- by mG
Hi everyone,
As the title says, I want to retrieve tables of data from a SQL database, using Flex 4 and .Net WebService.
I'm new to both Flex and DotNet. Please tell me a proper way to do it.
This is what I've done so far:
Retrieving an array of string: (this works)
.Net:
[WebMethod]
public String[] getTestArray()
{
    String[] arStr = { "AAA", "BBB", "CCC", "DDD" };
    return arStr;
}
Flex 4:
<?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">
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            import mx.rpc.events.ResultEvent;
            [Bindable]
            private var ac:ArrayCollection = new ArrayCollection();
            protected function btn_clickHandler(event:MouseEvent):void
            {
                ws.getTestArray();
            }           
            protected function ws_resultHandler(event:ResultEvent):void
            {
                ac = event.result as ArrayCollection;
                Alert.show(ac.toString());              
            }
        ]]>
    </fx:Script>
    <fx:Declarations>
        <s:WebService id="ws" wsdl="http://localhost:50582/Service1.asmx?WSDL" result="ws_resultHandler(event)"/>
    </fx:Declarations>
    <s:Button x="10" y="30" label="Button" id="btn" click="btn_clickHandler(event)"/>
</s:Application>
Retrieving a DataTable: (this does not work)
DotNet:
[WebMethod]
public DataTable getUsers()
{           
    DataTable dt = new DataTable("Users");
    SqlConnection conn = new SqlConnection("server = 192.168.1.50; database = MyDatabase; user id = sa; password = 1234; integrated security = false");         
    SqlDataAdapter da = new SqlDataAdapter("select vFName, vLName, vEmail from Users", conn);
    da.Fill(dt);
    return dt;
}
Flex 4:
<?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">
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            import mx.rpc.events.ResultEvent;
            [Bindable]
            private var ac:ArrayCollection = new ArrayCollection();
            protected function btn_clickHandler(event:MouseEvent):void
            {
                ws.getUsers();
            }           
            protected function ws_resultHandler(event:ResultEvent):void
            {
                ac = event.result as ArrayCollection;
                Alert.show(ac.toString());
            }
        ]]>
    </fx:Script>
    <fx:Declarations>
        <s:WebService id="ws" wsdl="http://localhost:50582/Service1.asmx?WSDL" result="ws_resultHandler(event)"/>
    </fx:Declarations>
    <s:Button x="10" y="30" label="Button" id="btn" click="btn_clickHandler(event)"/>
</s:Application>