Archive

Posts Tagged ‘call web service from flex’

Calling Web service from Flex – Part 1

June 23, 2009 5 comments

Here I am Explain how to use Web Services in Flex Application.

Steps:

1) First we create web service in Asp.net with C#. create new Web method in Web service is GetName.

[WebMethod]
    public string GetName(string UserName)
    {        
        return "WelCome  " + UserName;
    }

My web service name is WebServices.

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod]
    public string GetName(string UserName) {        
        return "WelCome  " + UserName;
    }
}

2) Open Flex Application.

3) Put below control in to application.

    3.1)  WebService (ID =MyWebService)

    3.2) Label for display Information

    3.3) TextArea (ID=txtUserName) – get value and pass value in to WebService

    3.4) Button (ID=btnSubmit) – Call Web service

    3.5) TextArea (ID=txtResult) – Display result from Web service

Application Page Info.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<!--Web Service Information 
wsdl  URL of Web Service
fault  if any error loading web service
operation  method name in Webservices
request  pass the parameter to Webservices
*here I get the TextBox Value 
-->
<mx:WebService id="MyWebService" wsdl="http://localhost:3549/ChangeWebConfigFile/WebService.asmx?WSDL"
    fault="mx.controls.Alert.show(event.fault.faultString)" >
    <mx:operation name="GetName">
        <mx:request>        
        <UserName>{txtUserName.text}</UserName>
        </mx:request>
    </mx:operation>
</mx:WebService>
<mx:VBox>

    <mx:Label text="Enter User Name">        
    </mx:Label>
    <mx:TextArea id="txtUserName" text="test">        
    </mx:TextArea>
    
    <mx:Button id="btnSubmit" label="Submit.." click="checkUserName();">        
    </mx:Button>
    <mx:TextArea id="txtResult"/>    
</mx:VBox>
<mx:Script>
    <![CDATA[
      private function checkUserName():void
    {
        MyWebService.GetName.send();
        txtResult.text= MyWebService.GetName.lastResult;            
    }                 
    ]]>
</mx:Script>
</mx:Application>

4) Run Flex Application.

 

image

 

 

thnx

Follow

Get every new post delivered to your Inbox.

Join 244 other followers