CONNECTING FLEX TO DATABASE USING C# .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 string HelloWorld()
{
SqlConnection con = new SqlConnection(“Data Source=SELVA\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True;”);
SqlCommand cmd = new SqlCommand();
con.Open();
cmd.CommandText = “SELECT * FROM student”;
cmd.Connection = con;
SqlDataAdapter ad = new SqlDataAdapter();
DataSet ds = new DataSet();
ds.DataSetName = “root”;
ad.SelectCommand = cmd;
ad.Fill(ds, “student”);
return ds.GetXml();
}
}
}
EXPLANATION:

Open visual studio and select new asp.net web service as project type.
now edit the hello world function in that to create a sql connection and buid a commandstring and dataadapter.
now create a dataset ds and fill the dataadapter using the dataset.finally return the data in XML format using appropriate method.

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.controls.TextInput;
import mx.controls.Text;
import mx.rpc.events.FaultEvent;
import mx.rpc.soap.WebService;
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
[Bindable]
private var dat:XML;
private function han():void
{
var ser:WebService=new WebService();
ser.addEventListener(ResultEvent.RESULT,dis);
ser.addEventListener(FaultEvent.FAULT,fal);
ser.loadWSDL(“http://localhost:2885/Service1.asmx?wsdl”);
ser.HelloWorld();
}
private function dis(event:ResultEvent):void
{
dat=XML(event.result);
adg1.visible=true;
}
private function fal(event:FaultEvent):void
{
Alert.show(“err”);
}
]]>
</mx:Script>

<mx:Button x=”188″ y=”169″ label=”Button” click=”han()”/>
<mx:AdvancedDataGrid visible=”false” dataProvider=”{dat.student}”  x=”54″ y=”199″ id=”adg1″ designViewDataType=”flat” height=”120″ width=”228″>
<mx:columns>
<mx:AdvancedDataGridColumn headerText=”name” dataField=”name”/>
<mx:AdvancedDataGridColumn headerText=”regno” dataField=”reg”/>
</mx:columns>
</mx:AdvancedDataGrid>

</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 returned by the web service is populated in the datagrid.
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 put the result in “dat” a XML variable.
Next i have populated the datagrid with the XML data i have got from the web service.

note:
u have to call the hello world function of the web service in the han() function
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.

any queries comment here.

  1. No comments yet.
  1. No trackbacks yet.

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.