Cannot implicitly convert type 'ServiceReference1.StockData[] ' to Systems.Collections.Generic.List<StockData>

10,844

When you added the reference to your site then under DataType ==> Collection Type you specified System.Array, (which is default as well), that is why your proxy is returning you an array instead of list.

enter image description here

When adding reference to the web service go to advance and specify System.Collection.Generic.List and you will get the same return type as in your contract.

But if you don't want to do that you can still use the Array and convert it to List using ToList

EDIT:

Like:

List<StockData> list = new List<StockData>();
list=(myProxy.orderStockData(txtinput1.Text, txtinput2.Text, txtinput3.Text)).ToList();
Share:
10,844
seeker
Author by

seeker

Craving for knowledge and whatever follows as a result . :)

Updated on August 25, 2022

Comments

  • seeker
    seeker over 1 year

    I'm trying to consume a WCF service that returns a custom list in the form List<StockData>.

    Here's the method signature from IService.cs:

     [OperationContract]
         List<StockData> orderStockData(string compName1, string compName2, string compName3);
    

    But when I try referencing it in my website through a service reference:

    List<StockData> list = new List<StockData>();
    list = myProxy.orderStockData(txtinput1.Text, txtinput2.Text, txtinput3.Text);
    

    I get the following error:

    Cannot implicitly convert type 'ServiceReference1.StockData[] ' to Systems.Collections.Generic.List

    Any help with solving this would be great. Thanks!

    • Seng Cheong
      Seng Cheong about 11 years
      Why are you creating a new List<>, just to assign a different result to that reference? Just do var list = myProxy....
    • seeker
      seeker about 11 years
      @JonathonReinhart, that gets rid of the error, but I'm not being able to access the elements of list later!
    • Seng Cheong
      Seng Cheong about 11 years
      Why not? What's not working?
    • seeker
      seeker about 11 years
      @JonathonReinhart: Ive placed both the code of both the service implementation and the proxy. The proxy now throws an index out of bounds error Iservice.cs :gist.github.com/KodeSeeker/5219192 Aspx.cs: gist.github.com/KodeSeeker/5219195
  • seeker
    seeker about 11 years
    How exactly do you pull up this screen? As in from where can I access it
  • seeker
    seeker about 11 years
    ` IList<StockData> list; list=myProxy.orderStockData(txtinput1.Text, txtinput2.Text, txtinput3.Text);` Doesnt work either
  • Habib
    Habib about 11 years
    How did you add the Service References ??usually, right click on the Service References under project in Visual studio, Add service reference, there you will find a button Advanced, from there you can find that screen. You can even click on your existing service reference and click update, see if you find the advanced button there.
  • Habib
    Habib about 11 years
    @KodeSeeker, you can also check the edited part of the answer, where you can convert the array to List
  • seeker
    seeker about 11 years
    @Habib- I tried the converting array to list part but it throws an error saying - Cannot implicilty convert type Systems.Collections.Generic.List<ServiceReference1.StockData‌​> to Systems.Collections.Generic.List<StockData>
  • seeker
    seeker about 11 years
    Also when I click update, it automatically downloads the service information once again ,there is no update option.
  • Habib
    Habib about 11 years
    @KodeSeeker, that is because the proxy generated its own class, Try if you can get to the above screen, there is an option to reuse types in all assembillies, check that as well.
  • Habib
    Habib about 11 years
  • Habib
    Habib about 11 years
    @KodeSeeker, remove the reference and try adding it again
  • seeker
    seeker about 11 years
    I tried setting the return type to System.Collection.Generic.List in the advanced settings but I get this error again Cannot implicilty convert type Systems.Collections.Generic.List<ServiceReference1.StockData‌​> to Systems.Collections.Generic.List<StockData>
  • Habib
    Habib about 11 years