10 Replies Latest reply on Mar 12, 2008 3:56 PM by CJSteu

    Desperate .Net Flex 2 problems

    VitaltypeMike
      I have been working on this problem on and off for over 2 months and I can't get a VB asp.net web service to communicate with a flex 2 datagrid. I followed the lynda.com training to the letter and still could not get it to work. I am running out of options and don't want to use Blend to create a RIA.

      Here is my VB webservice;

      Public Function getData() As System.Data.DataTable
      Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=(local);Initial Catalog=TTE_Intranet;Integrated Security=True")
      Dim ds As New System.Data.DataSet()
      Dim adapter As New System.Data.SqlClient.SqlDataAdapter()
      adapter.SelectCommand = New System.Data.SqlClient.SqlCommand("Select * From Typists", conn)
      adapter.Fill(ds, "Typists")
      Return ds.Tables("Typists")
      End Function

      Here is my MXML App. This is cut and paste from the Lynda,com training.

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
      <mx:Script>
      <![CDATA[
      import mx.utils.ArrayUtil;
      import mx.collections.ArrayCollection;
      import mx.rpc.events.ResultEvent;

      [Bindable]
      private var myData:ArrayCollection;
      private function dataHandler(event:ResultEvent):void
      {
      myData = event.result.diffgram.NewDataSet.Table;
      }
      ]]>
      </mx:Script>
      <mx:WebService id="dataService"
      wsdl=" http://localhost:2000/Intranet/DataGridTest.asmx?wsdl"
      showBusyCursor="true">
      <mx:operation name="getData" result="dataHandler(event)"/>
      </mx:WebService>
      <mx:DataGrid dataProvider="{myData}" y="30"/>
      <mx:Button label="Submit" click="dataService.getData()"/>

      </mx:Application>

      If anyone knows of a working example of a VB.net webservice talking to a flex 2 datagrid, I would like to see it. Thanks, Mike
        • 1. Re: Desperate .Net Flex 2 problems
          batmitra Level 1
          hi
          you're in luck, i'm a vb.net programmer and i'm developing a project in flex and i have many webservices working with flex controls, including datagrid.

          I don't know what tutorial from Lynda you were looking for but i've learned from Lynda one of the best way to do it and that's how i'm doing, i took it from Lynda's Flex 2 Beyond the basics tutorial, so here it is how you should do it:

          1 st- I use a Data transfer object, what is this , well it's simple , is a class from .net that has all the fields i need to send to flex

          Supose you want to send a customer data to flex, you must have a class like this:

          class customer
          public name as string
          public address as string

          end class

          and you must create that same class on flex


          2nd - I use an array of classes to encapsulate my data and send it to flex

          3rd- In flex you must get your result , and fill an arraycollection with it


          I don't have any code here but tomorrow , i promise i'll put some code in here.
          It's very easy to comunicate from felx to .net and viceversa, you'll see
          • 2. Re: Desperate .Net Flex 2 problems
            VitaltypeMike Level 1
            I cannot begin to tell you how much I appreciate that. I just want to see one datagrid work and I can go from there. Thanks again and looking forward to tomorrow.
            • 3. Re: Desperate .Net Flex 2 problems
              ntsiii Level 3
              If you do not need the third party discovery provided by the wsdl, consider HTTPService. It is much easier to work with than WebService and soap.

              Tracy
              • 4. Re: Desperate .Net Flex 2 problems
                bazjapan
                As mentioned build your classes on both he .net and flex side then call the service.
                Below puts them into an array collection.

                private var theArrayCollection:ArrayCollection= new :ArrayCollection();

                public function result(data:Object):void
                {
                var result:ResultEvent = data as ResultEvent;
                if(result.result != null){
                var theObjects:ArrayCollection = result.result as ArrayCollection;
                for (var i:int=0; i<theObjects.length; i++)
                {
                var currentItem:UserVO = new UserVO()
                currentItem = new UserVO(theObjects.getItemAt(i));
                theObjects.setItemAt(currentItem, i);
                }
                theArrayCollection = theObjects;


                }else{
                //do something if there are no objects in the result
                }
                }

                Hope that can put you on the right track.
                Also, you might want to check your webservice is actually returning the data correctly.

                Baz
                • 5. Desperate .Net Flex 2 problems
                  batmitra Level 1
                  Hi
                  i'm back to put here some code, but 1st let me say that Tracy is right ,HttpServices are much more easier to work with but i gave dealing with them , because there's one thing i need that httpservice doesn't do easily and that is call a methood directly like in webservice, so i've decided to implement my application width webservices.
                  so back to code:

                  the .net part
                  <WebMethod> _
                  Public Function Get_Customers(ByVal Name As String) As customer()


                  'here i'm getting my connection string from webconfig

                  Dim myconn As New SqlConnection(ConfigurationSettings.AppSettings("strconnect"))

                  Dim mycomm As New SqlCommand
                  Dim myda As New SqlDataAdapter
                  Dim myds As New DataSet


                  mycomm.CommandType = CommandType.StoredProcedure
                  mycomm.Connection = myconn
                  mycomm.CommandText = "yourstoredProcedure"
                  mycomm.Parameters.Add("@Name", Name)
                  myconn.Open()
                  myda.SelectCommand = mycomm
                  myda.Fill(myds)
                  myconn.Close()
                  myconn.Dispose()

                  Dim i As Integer
                  i = 0

                  Dim Names As Customer() = New customer(myds.Tables(0).Rows.Count - 1) {}
                  Dim aRow As DataRow

                  For Each aRow In myds.Tables(0).Rows
                  Names(i) = New Customer
                  Names(i).Name = aRow("Write here your dataset column name for customer name")
                  Names(i).Address = aRow("Write here your dataset column name for customer Address")

                  i += 1
                  Next

                  Return Names
                  End Function

                  Now you must have a class, that you build inside you webservice file, that works as your Data Transfer Object, so here it is

                  Public Class customer

                  Public Name as String
                  Public Address as String

                  end class

                  Now that you have your data transfer object let's read it on Flex, so here is the flex part

                  1st you must create a class that calls it self customer like the one in .net, you create this by going to file->new->actionscript class.There's a small difference between this class and the .net class, here it is:

                  package DTO //This means that my class is inside a folder called DTO, inside my application
                  {
                  [Bindable] 'The class must be bindable so this works well
                  public class customer
                  {
                  public var Name:String="";
                  public var Address:String="";

                  public function customer(obj:Object=null){
                  if (obj!=null){
                  this.Name=obj.Name; //I am supposing that your name collumn on webservice calls it self NAME
                  this.Address=obj.Address; //Once again the collumn that comes from webservice must be called ADDRESS


                  }
                  }
                  }
                  }


                  So now that we have our class, comes the fun part, lets put it on a grid


                  on my application file i put this :

                  <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute"
                  creationComplete="WsCustomers.Get_Customers()" >

                  <mx:Script>
                  <![CDATA[

                  import DTO.customer;

                  import mx.rpc.events.ResultEvent;

                  import mx.collections.ArrayCollection;

                  [Bindable]
                  public var Array_Customers:ArrayCollection;

                  //This is your most important part, building the array collection
                  private function Customers_result(e:ResultEvent):void{

                  Array_Customers = e.result as ArrayCollection;
                  for (var i:int=0;i<Array_Customers.length;i++)
                  { var Current_customer:customer = new customer();
                  Current_customer = new customer(Array_customers.getItemAt(i));
                  Array_customers.setItemAt(Current_customer, i);
                  }
                  }


                  <mx:WebService id="WsCustomers" wsdl="mywebserviceurl" showBusyCursor="true">
                  <mx:operation name="Get_Customers" result="Customers_result(event)" />
                  </mx:WebService>

                  <mx:DataGrid id="dg_Customers" dataProvider="{Array_Customers}" textAlign="center"
                  left="0" top="33" width="100%" height="90%">
                  <mx:columns>
                  <mx:DataGridColumn headerText="Name" dataField="Name" />
                  <mx:DataGridColumn headerText="Address" dataField="Address" width="50"/>
                  </mx:columns>
                  </mx:DataGrid>

                  so here it is all you need to talk with flex an .net, this way will work with both .net 1.x and 2.x versions of .net, because flex can't comunicate directly with webservice of .net 1.x and that is a known issue on Adobe.

                  Let just say 1 more thing, pay attention to case sensitive because i wrote this code very quickly but it can have mistakes and probably wil have to do with case sensitive. Have a nice programming day :D

                  • 6. Re: Desperate .Net Flex 2 problems
                    VitaltypeMike Level 1
                    This is great. I am trying this right now. The only questions I have is on the .net side. I am not an expert at VB classes, but I thought a function had to be within a class. Do you need two classes like this? Thanks again, this is what I am looking for.

                    Public Class DataGridTest
                    Inherits System.Web.Services.WebService
                    Public TypistUserName As String
                    Public TypistEmail As String
                    <WebMethod()> _
                    Public Function getData() As DataSet
                    Dim conn As New SqlConnection("Data Source=(local);Initial Catalog=TTE_Intranet;Integrated Security=True")
                    Dim ds As New DataSet()
                    Dim adapter As New SqlDataAdapter()
                    adapter.SelectCommand = New SqlCommand("Select TypistUserName, TypistEmail From Typists", conn)
                    adapter.Fill(ds, "Typists")
                    Return ds
                    End Function
                    End Class

                    Public Class Typists
                    Public TypistUserName as String
                    Public TypistEmail as String
                    End Class
                    • 7. Re: Desperate .Net Flex 2 problems
                      VitaltypeMike Level 1
                      Wow, that was a lot of code. I having everything compiling without errors, but I am noticing my .net web service is not returning any data. I have an error when I invoke it through a web browser. I am not sure if that class/function piece through me off.

                      Public Class DataGridTest
                      Inherits System.Web.Services.WebService
                      <WebMethod()> _
                      Public Function Get_Typists() As Typists()
                      Dim conn As New SqlConnection("Data Source=(local);Initial Catalog=TTE_Intranet;Integrated Security=True")
                      Dim ds As New DataSet()
                      Dim adapter As New SqlDataAdapter()
                      adapter.SelectCommand = New SqlCommand("Select TypistUserName, TypistEmail From Typists", conn)
                      adapter.Fill(ds, "Typists")

                      Dim i As Integer
                      i = 0

                      Dim Typist As Typists() = New Typists(ds.Tables(0).Rows.Count - 1) {}
                      Dim aRow As DataRow

                      For Each aRow In ds.Tables(0).Rows
                      Typist(i) = New Typists
                      Typist(i).TypistUserName = aRow("TypistUserName")
                      Typist(i).TypistEmail = aRow("TypistEmail")

                      i += 1
                      Next
                      Return Typist
                      End Function
                      End Class

                      Public Class Typists
                      Public TypistUserName As String
                      Public TypistEmail As String
                      End Class
                      • 8. Re: Desperate .Net Flex 2 problems
                        batmitra Level 1
                        quote:

                        Originally posted by: VitaltypeMike
                        Wow, that was a lot of code. I having everything compiling without errors, but I am noticing my .net web service is not returning any data. I have an error when I invoke it through a web browser. I am not sure if that class/function piece through me off.

                        Public Class DataGridTest
                        Inherits System.Web.Services.WebService
                        <WebMethod()> _
                        Public Function Get_Typists() As Typists()
                        Dim conn As New SqlConnection("Data Source=(local);Initial Catalog=TTE_Intranet;Integrated Security=True")
                        Dim ds As New DataSet()
                        Dim adapter As New SqlDataAdapter()
                        adapter.SelectCommand = New SqlCommand("Select TypistUserName, TypistEmail From Typists", conn)
                        adapter.Fill(ds, "Typists")

                        Dim i As Integer
                        i = 0

                        Dim Typist As Typists() = New Typists(ds.Tables(0).Rows.Count - 1) {}
                        Dim aRow As DataRow

                        For Each aRow In ds.Tables(0).Rows
                        Typist(i) = New Typists
                        Typist(i).TypistUserName = aRow("TypistUserName")
                        Typist(i).TypistEmail = aRow("TypistEmail")

                        i += 1
                        Next
                        Return Typist
                        End Function
                        End Class

                        Public Class Typists
                        Public TypistUserName As String
                        Public TypistEmail As String
                        End Class


                        Hi
                        from what i've seen on the code you've posted you are not doing things right, all you have to do is put this on your code behind file:

                        at the beginning:

                        Import system
                        import data.sqlclient
                        import system.web

                        then you write your function inside the main class wich was created when you created the asmx file

                        this would be this one

                        Public Class Datagridtest
                        <WebMethod> _
                        public function Get_Typists() As Typists()
                        end function
                        End class
                        then after this class you put your class like you have done
                        returning any data. I have an error when I invoke it through a web browser. I am not sure if that class/function piece through me off.

                        Public Class DataGridTest
                        Inherits System.Web.Services.WebService
                        <WebMethod()> _
                        Public Function Get_Typists() As Typists()
                        Dim conn As New SqlConnection("Data Source=(local);Initial Catalog=TTE_Intranet;Integrated Security=True")
                        Dim ds As New DataSet()
                        Dim adapter As New SqlDataAdapter()
                        adapter.SelectCommand = New SqlCommand("Select TypistUserName, TypistEmail From Typists", conn)
                        adapter.Fill(ds, "Typists")

                        Dim i As Integer
                        i = 0

                        Dim Typist As Typists() = New Typists(ds.Tables(0).Rows.Count - 1) {}
                        Dim aRow As DataRow

                        For Each aRow In ds.Tables(0).Rows
                        Typist(i) = New Typists
                        Typist(i).TypistUserName = aRow("TypistUserName")
                        Typist(i).TypistEmail = aRow("TypistEmail")

                        i += 1
                        Next
                        Return Typist
                        End Function
                        End Class

                        Public Class Typists
                        Public TypistUserName As String
                        Public TypistEmail As String
                        End Class

                        now test it, 1st see if the webservice is running on your browser, if it shows the method, then invoke the method so that you see if it returns data, i've notice that your connection string is pointing to a local server, it should be localhost without the (). Are you sure that the columns returned on your dataset have the same names as your Typists class atributes? Because they don't need to have the same names as you may know.
                        so test it and let me know your progress , i don't you to spend another 2 months to put this easy thing to work.
                        • 9. Re: Desperate .Net Flex 2 problems
                          VitaltypeMike Level 1
                          I got it to work using VB. I am not sure the difference, but it is a lot less code.

                          Here is the VB.net webservice.

                          Imports System.Web
                          Imports System.Web.Services
                          Imports System.Web.Services.Protocols
                          Imports System.Data
                          Imports System.Data.SQLClient

                          ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
                          ' <System.Web.Script.Services.ScriptService()> _
                          <WebService(Namespace:=" http://tempuri.org/")> _
                          <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
                          <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
                          Public Class ServiceVB
                          Inherits System.Web.Services.WebService
                          Private connString As New SqlConnection("Data Source=(local);Initial Catalog=TTE_Intranet;Integrated Security=True")

                          <WebMethod()> _
                          Public Function getData() As DataTable
                          Dim ds As New DataSet()
                          Dim adapter As New SqlDataAdapter()
                          adapter.SelectCommand = New SqlCommand("Select TypistUserName, TypistEmail From Typists", connString)
                          adapter.Fill(ds)
                          Return ds.Tables(0)
                          End Function

                          End Class

                          Here is the MXML file. No AS class was needed this way.

                          <?xml version="1.0" encoding="utf-8"?>
                          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical">
                          <mx:Script>
                          <![CDATA[
                          import mx.collections.ArrayCollection;
                          import mx.rpc.events.ResultEvent;

                          [Bindable]
                          private var myData:ArrayCollection;

                          private function dataHandler(event:ResultEvent):void
                          {
                          myData = event.result.diffgram.NewDataSet.Table;
                          }
                          ]]>
                          </mx:Script>
                          <mx:WebService id="dataService"
                          wsdl=" http://localhost:1333/TTEIntranetVB/ServiceVB.asmx?wsdl"
                          showBusyCursor="true">
                          <mx:operation name="getData" result="dataHandler(event)"/>
                          </mx:WebService>
                          <mx:DataGrid dataProvider="{myData}">
                          <mx:columns>
                          <mx:DataGridColumn dataField="TypistUserName" headerText="Username"/>
                          <mx:DataGridColumn dataField="TypistEmail" headerText="Email" width="200"/>
                          </mx:columns>
                          </mx:DataGrid>
                          <mx:Button label="Submit" click="dataService.getData()"/>
                          </mx:Application>
                          • 10. Re: Desperate .Net Flex 2 problems
                            CJSteu
                            I just want to thank you for posting this. You have solved sooo many problems... so well, and there is very, very few examples of this out there.
                            Craig