Flex calls the WebService datatable results in the writing of DataGrid

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The WebService configuration file

	<appSettings>
		<add key="sqlConDuke" value="server=10.9.34.88;database=PanoSalado;uid=sa;pwd=123"/>
	</appSettings>
Webservice code

 //The test database connection
    [WebMethod(Description = "!!!The test database connection, the connection string in the web.config")]
    public bool conectDB()
    {
        strCon = ConfigurationSettings.AppSettings["sqlConDuke"];
        conn = new SqlConnection(strCon);
        try
        {
            conn.Open();
            return true;
        }
        catch (Exception)
        {
            return false;
        }
        finally
        {
            conn.Close();
        }
    }

    [WebMethod(Description = "!!!Access to all data(DataTable), strTb,eg:PanView")]
    public DataTable getAllDT(string strTb)
    {
        bool ifCon = conectDB();
        if (ifCon)
        {
            string sql_BfCar = "SELECT * FROM " + strTb;
            da = new SqlDataAdapter(sql_BfCar, conn);
            myds = new DataSet();
            da.Fill(myds);
            mydt=myds.Tables[0];
            return mydt;
        }
        else
        {
            return null;
        }
    }

Flex code:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx=" ; 
			   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.FaultEvent;
			import mx.rpc.events.ResultEvent;

			[Bindable]
			public var arr:ArrayCollection=new ArrayCollection();
			
			protected function button1_clickHandler(event:MouseEvent):void
			{
				myWeb.getAllDT("PanView"); //Are methods and parameters in webservice
			}
			internal function onSuccess(evt:ResultEvent):void
			{
				 arr=this.myWeb.getAllDT.lastResult.Tables.Table.Rows; //Table WebService datatable name
				 Alert.show(arr.length.toString());
			}

			protected function button2_clickHandler(event:MouseEvent):void
			{
				myWeb.getLinkDTByImgName("PanView","20131019072740-0000150");
			}
			internal function onSuccess1(evt:ResultEvent):void
			{
				arr=this.myWeb.getLinkDTByImgName.lastResult.Tables.Table.Rows;
				Alert.show(arr.length.toString());
			}
			
			internal function onFault(evt:FaultEvent):void
			{
				Alert.show("Failed to call the WebService method, detailed: " + evt.fault.faultDetail.toString());
			}

		]]>
	</fx:Script>
	<s:layout>
		<s:BasicLayout/>
	</s:layout>
	<fx:Declarations>
		<!-- non visual elements (e.g., service, value object) here -->
		<s:WebService id="myWeb" wsdl=" ; fault="onFault(event)">
			<s:operation name="getAllDT" result="onSuccess(event)"></s:operation>
			<s:operation name="getLinkDTByImgName" result="onSuccess1(event)"></s:operation>
		</s:WebService>
	</fx:Declarations>
	<mx:DataGrid x="121" y="177" width="737" height="402" id="dg1" dataProvider="{arr}"> 
		<mx:columns>
			<mx:DataGridColumn headerText="ID" dataField="ID"/> <!Field -- datatable --webservice>
			<mx:DataGridColumn headerText="ImageName" dataField="ImageName"/>
			<mx:DataGridColumn headerText="LinkGuid" dataField="LinkGuid"/>
		</mx:columns>
	</mx:DataGrid>
	<s:Button x="254" y="82" label="Returns all" click="button1_clickHandler(event)"/>
	<s:Button x="500" y="82" label="According to the query" click="button2_clickHandler(event)"/>
</s:Application>



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Isidore at December 23, 2013 - 4:40 PM