Posted by: Vinit on: June 27, 2011
Hi,
First Create the database in Sql.
After that create web service in asp.net with c#.
the code is like below.
public class LoginDetails : System.Web.Services.WebService
{
SqlConnection con = new SqlConnection(“Data Source=SourceName;Initial Catalog=DBName;User id=UserName;Password=Password”);
public LoginDetails()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
// Method to get login records from database
[WebMethod]
public bool GetLoginDetails(string UserName, string Password)
{
// Method not followed any coding standards. This is just for demo purpose
var da = new SqlDataAdapter(“SELECT * from Login where Username =’”+UserName+”‘ and Password=’”+Password+”‘” , con);
var dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
return true;
}
else
{
return false;
}
}
}
// Login Class
public class Login
{
public string LoginId = string.Empty;
public string UserName = string.Empty;
public string Password = string.Empty;
}
After creating service just put the reference of that service in that .net application.
flex Code:-
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”init()”>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.core.mx_internal;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private function LoginUser(event:MouseEvent):void {
ws.GetLoginDetails(txtUserName.text, txtPassword.text);
}
private function GetLoginDetails(event:ResultEvent):void {
// Save a record using a WebService method
if(event.result == 1)
{
Alert.show("login successful");
}
else
{
Alert.show("login unsucessful");
}
}
private function fault(event:FaultEvent):void {
// Oppps some error occured
Alert.show(event.toString());
}
]]>
</mx:Script>
<!– WebService definition –>
<mx:WebService id=”ws” wsdl=”http://localhost:6030/TestWebService/LoginDetails.asmx?wsdl” fault=”fault(event)”>
<mx:operation
name=”GetLoginDetails”
resultFormat=”object”
result=”GetLoginDetails(event)”
/>
</mx:WebService>
<mx:Panel x=”331.5″ y=”166″ width=”270″ height=”150″ layout=”absolute” title=”ASP.NET WebService + Flex Login Demo”>
<mx:HBox height=”95%” width=”95%” horizontalCenter=”0″ verticalCenter=”0″>
<mx:Form x=”100″ y=”100″ width=”100%” height=”100%” borderStyle=”solid”>
<mx:FormItem label=”UserName”>
<mx:TextInput width=”106″ id=”txtUserName”/>
</mx:FormItem>
<mx:FormItem label=”Password”>
<mx:TextInput width=”106″ id=”txtPassword”/>
</mx:FormItem>
<mx:FormItem width=”156″ horizontalAlign=”right”>
<mx:Button label=”Login” id=”btnLogin” click=”LoginUser(event)”/>
</mx:FormItem>
</mx:Form>
</mx:HBox>
</mx:Panel>
</mx:Application>
In the above code put the service reference URL replacing “http://localhost:6030/TestWebService/LoginDetails.asmx?wsdl“.
and run the application you have below screen.
Now you can have UserName : Test & Password : Test
Then the screen appears like…
and if you enter Username : OtherTest & Password : Test
The screen appears like…
So, this way you can use asp.net web services in the Flex. For more query and details you can contact me or ask me freely.
Comments