Calling Web service from Flex – Part 1

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

Advertisements

5 comments

  1. nice article..

  2. Very helpful document for flex learners and beginners..

  3. youre about the only person Ive seen in about the past six or so nights that actually checks their code before they post it. Man, is it just me or are the vast bulk of online tutorials these days just junk posted for a laugh or something?

    1. I am not getting you…

  4. i run the application i get a error message “Security error accessing url”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s