updating the database from flex using asp.net
c# WEB SERVICE CODING:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
namespace WebService2
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public void save(string nam, string re)
{
SqlConnection con = new SqlConnection(“Data Source=SELVA\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True;”);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = “INSERT INTO student(name,reg)VALUES(@nam,@re)”;
cmd.Connection = con;
cmd.Parameters.Add(new SqlParameter(“@nam”,nam));
cmd.Parameters.Add(new SqlParameter(“@re”,re));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
EXPLANATION:
Open visual studio and select new asp.net web service as project type.
now create a save function with two arguments that are to be updated in the database .next create a sql connection and buid a commandstring and execute the query.
FLEX CODING:
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute”>
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.soap.WebService;
import mx.controls.Alert;
private function han():void
{
var we:WebService = new WebService();
we.addEventListener(ResultEvent.RESULT,up);
we.addEventListener(FaultEvent.FAULT,fa);
we.loadWSDL("http://localhost:2885/Service1.asmx?wsdl");
we.save(nam.text,re.text);
}
private function up(event:ResultEvent):void
{
Alert.show("sucess");
}
private function fa(event:FaultEvent):void
{
Alert.show(event.toString());
}
]]>
</mx:Script>
<mx:TextInput x=”179″ y=”50″ id=”nam”/>
<mx:Label x=”104″ y=”52″ text=”name”/>
<mx:Label x=”106″ y=”109″ text=”reg”/>
<mx:TextInput x=”179″ y=”109″ id=”re”/>
<mx:Button x=”179″ y=”188″ label=”Button” click=”han()”/>
</mx:Application>
EXPLANATION:
Here iam using the web service to connect to the asp.net webservice written in c#.
In this i have a button when u click the button the data from the two textbox “nam” and “re” will be updated in the database .
I have created the web service in the button handler function “han()” and i have added listeners for fault event and result event.In the result event i have an alert box indicating sucess.
note:
u have to call the save function of the web service in the han() function with textboxes text as arguments
u have to set the url of the load method of the web service
the database i used is a student database with name and reg field.
feedback