none
How to return data from WCF service to populate dropdownlist

    Question

  •  

    What is the proper way to make this work?

     

    I want to call a WCF service method that should return data (in whatever format is appropriate) that I can then bind to a dropdownlist in ASP.NET. 

     

    I have tried using List<string> and Collection<string>, but when I go to configure my object data source, the only choice that I get for my display and value data fields is "Length"

     

    I would prefer to be able to return both the "Name" and the "ID" from my database for each item, but I don't know what format to use to send the data from the WCF service that is appropriate for serialization, but also consumable for my ASP.NET app...

     

    please help!

    Wednesday, April 09, 2008 8:58 PM

Answers

  • You could certainly use a more complex type (well, more complex than string) and a data contract, something like:

     

    [DataContract]

    public class MyData

    {

    [DataMember]

    public string Name

    {

    get;

    set;

    }

     

    [DataMember]

    public string Id

    {

    get;

    set;

    }

    }

     

    Then have your service return a list or collection of this type.

     

    I'm not sure what you've heard about datasets, there is information on using types like DataSet with the DataContractSerializer (the default serializer for wcf) here:

    http://msdn2.microsoft.com/en-us/library/aa347876.aspx

     

    Friday, April 11, 2008 9:09 PM
  • Here's what I did to bind data from a WCF service to a dropdown list:

     

    (1) Create a DataContract for a class (here called MyData) that I want to fill the rows of my dropdownlist

    (2) Create a service contract to return a list of this class from the database, with a signature like:

     

    [ServiceContract]

    public interface IService1

    {

    [OperationContract]

    List<MyData> GetData();

    }

     

    (3) Create a service reference to the WCF service

     

     

    (4) In the forms designer, choose "Choose data Source" from the dropdown next to the control, create a new object data source, choose the client class from your service reference as the class, choose the appropriate method in the "select" pane (here it's GetData), and finally choose the fields for display and value from the provided drop down.

     

    Note that you could also write a contract that returned a dataset and do a similar thing.  You might also create your own business object to manage creation of the client and use this as the data source, but the principle is the same.

     

     

    Tuesday, April 15, 2008 6:27 PM

All replies

  • The return type through the generated proxy is probably just an array and not a List.  You could create a list in the client like so:

     

    List<string> results = new List<string>();

    results.AddRange(wcfResults);

     

    Then just bind to this.  Or if you are using Visual Studio 2008, you can use the new "Add Service Reference" window, navigate to the endpoint, select the advanced tab, and have it return collections as type List or ArrayList of whatever.  That way its more easily bindable.

     

    However even if the wcf method returns just an array you should still be able to bind to the array itself.

     

    Wednesday, April 09, 2008 9:53 PM
    Moderator
  • I am using VS2008, and I tried setting the return values to a few different available settings:  ArrayList, List, LinkedList, BindingList.

     

    Each one that I pick shows up as a different return type when I choose the method in the "configure data source" window, but in the "Choose Data Source" window I only have "Length" available for my display and value fields.

     

    Do I need to have my WCF Service return something other than Collection<string> or List<string>?

     

    As I mentioned before, I would prefer to return more data anyway, so I am not apposed to returning a more complex data type, but unfortunately everything I read tells me that I cannot use a dataset, but nothing tells me what I can use...

     

    I basically want to return two columns of data, one row for each item in the dropdown list.  the "ID" column from the database to put into the value field of the dropdownlist item, and the "name" column from the database to put into the display field of the dropdownlist item.

     

    Do I need to create a more complex type?  Could using a datacontract help me?

    Thursday, April 10, 2008 8:00 PM
  • Which public properties (not fields) does your object returned in the list/array have ?

     

    Thursday, April 10, 2008 8:15 PM
  • You could certainly use a more complex type (well, more complex than string) and a data contract, something like:

     

    [DataContract]

    public class MyData

    {

    [DataMember]

    public string Name

    {

    get;

    set;

    }

     

    [DataMember]

    public string Id

    {

    get;

    set;

    }

    }

     

    Then have your service return a list or collection of this type.

     

    I'm not sure what you've heard about datasets, there is information on using types like DataSet with the DataContractSerializer (the default serializer for wcf) here:

    http://msdn2.microsoft.com/en-us/library/aa347876.aspx

     

    Friday, April 11, 2008 9:09 PM
  •  

    Well, I used a data contract much like the one you suggested, and I have those fields available to me in my data binding drop down lists, but I'm not getting any data returned from the service to the client...

     

    Any ideas?

    Tuesday, April 15, 2008 3:50 PM
  • Well, I used a data contract much like the one you suggested, and I have those fields available to me in my data binding drop down lists, but I'm not getting any data returned from the service to the client... Any ideas?
    Tuesday, April 15, 2008 3:51 PM
  • I found the problem;  Sort of....

     

    For some reason, the ReturnValue parameter for the receiveActivity had set itself back to nothing while I was messing around with this stuff, and when I click on the (...) elipsis button for the (ReturnValue), there weren't any members for me to choose from.  In fact, it was giving me a completely different window...

     

    I created another public member, that was just a simple 'int', rebuilt the app, then when I clicked the (...) button for the (ReturnValue) parameter this time, it gave me the window with the two tabs for binding to an existing member and binding to a new member.  I selected the int from the existing member tab, rebuilt, and everything was fine with my dummy int member.

     

    I clicked on the (...) ReturnValue again, and amazingly enough, my original List<customtype> was available in the existing member tab, so I chose it, rebuilt, and now my Client app is binding to the returned data as it is supposed to.

     

    I'm considering this done for now, but if anyone has any idea what I could have done wrong to cause this, I'd love to hear!

     

    Thanks everyone!

    Tuesday, April 15, 2008 6:25 PM
  • Here's what I did to bind data from a WCF service to a dropdown list:

     

    (1) Create a DataContract for a class (here called MyData) that I want to fill the rows of my dropdownlist

    (2) Create a service contract to return a list of this class from the database, with a signature like:

     

    [ServiceContract]

    public interface IService1

    {

    [OperationContract]

    List<MyData> GetData();

    }

     

    (3) Create a service reference to the WCF service

     

     

    (4) In the forms designer, choose "Choose data Source" from the dropdown next to the control, create a new object data source, choose the client class from your service reference as the class, choose the appropriate method in the "select" pane (here it's GetData), and finally choose the fields for display and value from the provided drop down.

     

    Note that you could also write a contract that returned a dataset and do a similar thing.  You might also create your own business object to manage creation of the client and use this as the data source, but the principle is the same.

     

     

    Tuesday, April 15, 2008 6:27 PM
  • As it turns out, that is pretty much exactly what I did.

     

    I still have one problem, and I can't get around it.

     

    Since I'm using a workflow service, the flow of my program goes like this:

    1. The receiveactivity of the workflow is initiated

    2. As of right now, I only have one "codeactivity" in the workflow, and it calls my DAL function.

    3. my DAL function populates my local member "List<MyData>"

    4. the ReturnValue parameter of my receiveactivity needs to be bound to "List<MyData>" but when I click on the (...) button to do this, it opens a "Collection Editor" window with nothing listed...

     

    If I change my service contract and everything else to a primitive type like int or string, then it works fine.  I get the "bind (ReturnValue) to an activities property" window, I select my local member, and everything works as it should.

     

    I wish I could figure this one out, it is the last thing in my way to kicking this project into full gear, and it's driving me nuts!

     

    Am I not allowed to bind to generic lists like that?  what am I doing wrong?

     

     

    Wednesday, April 16, 2008 4:36 AM
  • I found the solution to your problem.

    Click on the (ReturnValue) property in the property window when you select the receive activity. Then right click on the receive activity in the designer and select 'Bind (ReturnValue)' and bind the property to your list.

    Hope this helps!
    Friday, June 20, 2008 2:16 AM
  • Yes, that works.  I also found another solution:

     

    If, instead of clicking on the (...), you double click on the little golden cylinder next to the parameter name, it will open the correct dialogue box.  I'm not sure if they designed it this way on purpose, but it really seems stupid....

     

    Oh well, it's working for now.

     

    Thanks everyone!

     

    Friday, June 20, 2008 12:31 PM
  • Here is the link.. it is explained in detail.. http://gouthamreddyc.blogspot.com/2009/10/wcf.html
    • Proposed as answer by Goutham_reddyc Saturday, October 10, 2009 9:43 PM
    Saturday, October 10, 2009 9:43 PM