Cannot implicitly convert type 'ServiceReference1.StockData[] ' to Systems.Collections.Generic.List<StockData>
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.
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();
seeker
Craving for knowledge and whatever follows as a result . :)
Updated on August 25, 2022Comments
-
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 about 11 yearsWhy are you creating a
new List<>
, just to assign a different result to that reference? Just dovar list = myProxy....
-
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 about 11 yearsWhy not? What's not working?
-
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 about 11 yearsHow exactly do you pull up this screen? As in from where can I access it
-
seeker about 11 years` IList<StockData> list; list=myProxy.orderStockData(txtinput1.Text, txtinput2.Text, txtinput3.Text);` Doesnt work either
-
Habib about 11 yearsHow 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 about 11 years@KodeSeeker, you can also check the edited part of the answer, where you can convert the array to List
-
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 about 11 yearsAlso when I click update, it automatically downloads the service information once again ,there is no
update
option. -
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 about 11 years
-
Habib about 11 years@KodeSeeker, remove the reference and try adding it again
-
seeker about 11 yearsI 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 about 11 years@KodeSeeker, check out this post stackoverflow.com/questions/8286775/wcf-common-types-not-reused