Friday, December 12, 2008

Communicating Flex to DotNet using Objects

Flex can communicate with Dot Net using Web service. Now explain how to pass objects from Flex to Dot net and fore back.

The Declaration of the objects must be done either in Dot Net or Flex.

First Declare and declare the object that we have to access in Dotnet as Address.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

///
/// Summary description for custaddrinfo
///

public class addressinfo
{


public string offaddressline;
public string offplace;
public string offcity;
public string offstate;
public string offmobile;
public string offpostalcode;
public string offcountry;
public string offtelephone;
public string offfaxno;
public string offemailid;
public string offemerperson;
public string offemerperteleph;
public string offemerperobile;

public addressinfo()
{
// TODO: Add constructor logic here
}
}


In the Second Step Define the Web service (C#.Net) as

[WebMethod]
public string insert_contact_testing(addressinfo addr)
{

database_connection....and other statements..
insert into contacttable values (addr.offaddressline, addr.offpostalcode, addr.offplace, addr.offcity, addr.offstate, addr.offcountry, addr.offtelephone, addr.offmobile, addr.offfaxno, addr.offemailid, addr.offemerperson, addr.offemerperteleph, addr.offemerperobile);

}

Then in flex Initialize an object and bind values to it and Pass the values to DotNet using Webservice.


/////////////////////////////////////////////////////////////////////////////////////////////////////
//###############################creating object for address info #############################


public var addressinfo:Object;
public function crtobj():void
{
addressinfo= new Object();
//office address
addressinfo.offaddressline = Prj_Reg_CustAddressparty1text.text;
addressinfo.offpostalcode = Prj_Reg_CustAddressPin1text.text;
addressinfo.offplace = Prj_Reg_CustAddressplace1text.text;
addressinfo.offcity = Prj_Reg_CustAddressCity1text.text;
addressinfo.offstate = Prj_Reg_CustAddressState1text.text;
addressinfo.offcountry = Prj_Reg_CustAddresscountry1Text.text;
addressinfo.offtelephone = Prj_Reg_CustAddressTel1text.text;
addressinfo.offmobile = Prj_Reg_CustAddressMobile1Text.text;
addressinfo.offfaxno = Prj_Reg_CustAddressFax1Text.text;
addressinfo.offemailid = Prj_Reg_CustAddressMail1text.text;
addressinfo.offemerperson = Prj_Reg_CustAddressconperson1Text.text;
addressinfo.offemerperteleph = Prj_Reg_CustAddresscontel1Text.text;
addressinfo.offemerperobile = Prj_Reg_CustAddresscontmobile1Text.text;

}

//###################################### web service initialize ########################
private var prjRegService:WebService;

public function initService()
{
prjRegService = new mx.rpc.soap.WebService();
prjRegService.wsdl = 'http://';
}
//############################################ save datas to database ###################
public function saveCustAddrInfoDetails():void
{

initService();
prjRegService.insert_contact_testing.addEventListener("result",custaddrinfodetailsSave);
prjRegService.loadWSDL();
prjRegService.insert_contact_testing(addressinfo);

}
public function custaddrinfodetailsSave(event:ResultEvent):void
{

var res:String;
res=event.result.toString();
//Alert.show("result value" + res);

}
}

No comments: