Calling Web Service Function returns List (Of String) from Windows Form
The reason that happens is because your web service is not returning an actual .NET List
object, it's simply returning an XML representation of the list using the standard SOAP format for lists. As such, the type used to return the data on the client-side is completely determined by the code on the client.
When you add a reference to a web service, Visual Studio automatically generates proxy classes for you. The proxy classes are client-side replicas of the web methods defined on the server-side. However, for lists, the client-side has no way of knowing what specific .NET type of list was returned by the web method. As far as it knows, the web service may not have even been written in a .NET language as all. Therefore, it has to arbitrarily pick a way to represent the data in the proxy class. The default list type that is used in proxy classes is simple arrays.
When you are added the reference to the web service, if you add it as a Web Reference, you are not given an option to specify the type to use for lists. Web References always use arrays for lists. If, however, you are adding the reference as Service Reference, then you can change the type that will be used in the proxy classes to represent lists. To do so, when adding the reference, click the Advanced button. Then, in the Data Type section change the Collection type from System.Array
to System.Collections.Generic.List
.
If you are stuck leaving your proxy classes using arrays, you can still make it work by simply converting the array to a List
, like this:
Dim listOfGames As New List(Of String)()
listOfGames.AddRange(ws.getGames("username123", "password123"))
Dav
Updated on June 05, 2022Comments
-
Dav almost 2 years
I have a Web Service with multiple functions that do a job with SQL Server 2012 database. My aim is to have a Windows Service that uses this web service functions to select and insert to a database.
The current web service have different functions types that returns boolean, string or list of string. Everything works fine except when calling function that returns list of string getGames () from a Windows Form. (Windows Form as a replica for Windows Service)
Error: Value of type '1-dimensional array of Object' cannot be converted to 'System.collections.generic.list(Of String)'
From the Windows Form project I added a web service - Add Service Reference > Advanced > Add Web Reference. I am still a student so I don't have much experience on web services. Windows Service is created on Framework 3.5 and Windows Form on Framework 4.5 (not sure if this makes a difference) Error:
listOfGames = ws.getGames("username123", "password123") '** Error Here
Here's the web service function
<WebMethod()> _ Public Function getGames(ByVal username As String, ByVal password As String) As List(Of String) Dim m As DBMember = DBMember.verifyUsername(username, password) If m IsNot Nothing Then Dim gList As New List(Of String) gList = DBGame.selectAllGames() Return gList End If Return Nothing End Function
Here's how I'm calling the web service function:
Private Sub onStart() Dim ws As localhost.Service1 = New localhost.Service1 listOfGames = ws.getGames("username123", "password123") '** Error Here End Sub
Here's the query:
Shared Function selectAllGames() As List(Of String) Dim cmd As New SqlCommand cmd.CommandText = "SELECT g_Exe FROM Game" cmd.Connection = DB.Conn() Dim rdr As SqlDataReader = cmd.ExecuteReader() Dim gList As New List(Of String) If rdr.HasRows Then While rdr.Read gList.Add(rdr.Item("g_Exe").ToString()) End While End If DB.CloseDB() Return gList End Function
-
Dav about 11 yearsThank you so much Steve it worked by converting it to an array. Dim listOfGames As New List(Of String)() listOfGames.AddRange(ws.getGames("*", "")) For Each i In listOfGames lstStartUp.Items.Add(i) Next