Posted by: arunbluebrain on: December 11, 2008
In this tutorial Nate is going to show you how to pull data out of a SQL Server database with .NET/LINQ, create a web service that publishes the data and consume it with a Flex app. It’s actually pretty simple so let’s get going. (This example is done in the .NET 3.5 Framework)
.NET Setup
You can skip these steps if you are familiar with .NET and have a database that you want to pull information from.
The first thing we do is create a visual studio project. Select File -> New Project. Select ASP.NET Web Service and name the web service FlexDotNetHelloWorld.
Click on the server explorer tab and and right click on data connections. Select the server and database you want to connect to. I made a new database and called it CMDB1.
Expand the new database (or what ever database you want to work with) and right click on Tables and select “Add New Table”. We are going to add three fields. A first name, a last name and a primary key. You can see the fields I named below. Notice that I set NameID as the primary key and set (IsIdentity) to Yes. Save the table as CMTBL1.
In the server explorer right click on the table and select “Show Table Data”. We are going to put in some data so we have something to pull. You can put in what ever you want.
Setting up LINQ
Now it’s time to do our data connection setup. First we will create a datacontext file to pull from our database. We will be using LINQ for our data access which is completely amazing. If you don’t know much about it I suggest you check their LINQ Learning section at asp.net. In the solution explorer right click on your project name (FlexDotNetHelloWorld) and select add->New Item. Select LINQ to SQL Classes and name it CMDB.dbml.
That file should open for you. Now go back to your server explorer. Go into your database and find the CMTBL1 table we just made. Drag and drop that table into the left area; that’s called the object relational designer.
That’s it! We can now query from the table. LINQ can do A LOT cooler stuff than just this and I suggest you look into it!
Setting up the Webservice
Time to code. First we create a class so we can pass back a simple list. We could return our linq result but that would be inefficent. Create a new class called Person. Add in the nameID, firstName and lastName.
Public Class Person Public nameID As Integer Public firstName As String Public lastName As String End Class
Now we write our function to get our data. I use a generic to return the data. It’s a list of Person (that class we just made). Generics are pretty cool, it makes it really easy to transfer data when you do it like this. I do a little linq query then loop through it and start adding people to my return result.
<WebMethod()> _ Public Function GetNameList() As List(Of Person) Dim db As New CMDBDataContext() Dim returnResult As New List(Of Person) Dim result = From s In db.CMTBL1s _ Select s For Each item In result Dim obj As New Person obj.nameID = item.NameID obj.firstName = item.FName obj.lastName = item.LName returnResult.Add(obj) Next Return returnResult End Function
Now click the green debug button at the top. Click OK to debug. Select the GetNameList then click Invoke. Your data should look something like this.
Go back to the page where you invoked the function. Click on service description. The link that it sends you to is your WSDL. Write this down, http://localhost:41834/Service1.asmx?WSDL. Yours will be different. One thing to be careful of is that the port number on your localhost will change every time you run this app in a development server. If you run it in IIS you will have a static URL that you can rely on. You can also set up Visual Studio to always use the same port when starting your development server. This can be handy if you want to do a lot of local development.
Flex
I’m going to assume that you have some basic Flex knowledge about how to do things like set up a project, add a datagrid and bind data to it. If you need a more detailed explanation let me know.
Here’s the code you need to get the data.
<mx:WebService id="dotNetService" wsdl="http://localhost:41834/Service1.asmx?WSDL" useProxy="false" showBusyCursor="true"> <mx:operation name="GetNameList" result="GetNameListResult(event)"/> </mx:WebService>
Put your wsdl in where I put mine. The GetNameListResult is the function where you want to receive your data. Here’s what that code looks like.
[Bindable] private var nameList:Array() private function GetNameListResult(event:ResultEvent){ nameList = event.result }
I created an array to hold the data. You can then bind that to a datagrid or what ever you want. It’s really that simple.
One last thing to do. You have to call your webservice. To do that just use the id of the webservice like this.
dotNetService.GetNameList()
That’s it! You’ve now started down the wonderful path of .net to flex goodness. You can really do some powerful cool stuff. I really like the combination.
Originally from : Flex and .NET sharing a bunk bed by Nate
you can leave your comments and suggestions for Nate at http://www.19nates.com/2008/12/connecting-flex-with-net/
Hi,
When integrating LINQ with adobe flex web services, I am facing following issues.
In case of Inserting parent-child records, my .NET method has a parameter of LINQ type entity class, I create a Object in Adobe flex and assign properties and send it to web service method.
.net accepts this but ignores all the child properties. I tried with auto generated web service methods aslo but no luck. Please help me.
Please give an example for Insert with parent child relationship. That with help us lot.
December 12, 2008 at 12:18 am
Wow, you even got my website wrong! It’s http://www.19nates.com. The un-plagiarized version would be to post a link to my page and talk about it.
Just like this guy did: http://cftips.net/post.cfm/connecting-flex-3-to-asp-net-using-linq
December 12, 2008 at 11:15 am
Sorry and Excuse me Mr.Nate for providing the wrong link. but the link i provided is working.
I don’t want to make others believe that i have done this work.You have done a very good job.
please forgive me if i have done anything wrong.
Now i have updated my post with ur name and link u specified. I hav left an comment in your site on the post.
If u have some time now u can check out my post with updated links and leave your valuable suggestions if any.
http://arunbluebrain.wordpress.com/2008/12/11/connecting-flex-with-net-35-beginner/
Its a very good post for the people who want to start working on flex with .Net.
Thank u once again.
Arun