How to Consume a web service that returns an arraylist

S

Sam

Please I need help.

I am trying to call a web service method that returns an arraylist from my
windows application but I could not do it because of the following error
message:

(value of type '1-dimensional array of object' cannot be converted to
'system.collections.ArrayList')

I need just simply a piece of sample code that show me how to return an
arraylist from a web service method and how to consume it from the client
side the right way.

My web service Method is as follow:

<WebMethod(), XmlInclude(GetType(CustomerInfo))> _
Public Function CustomersList() As <XmlArray("CustList")> ArrayList
Dim _Result As New ArrayList

Dim oInfo1 As New CustomerInfo
oInfo1.CustomerNo = 1
oInfo1.CustomerName = "Customer1"
_Result.Add(oInfo1)

Dim oInfo2 As New CustomerInfo
oInfo2.CustomerNo = 2
oInfo2.CustomerName = "Customer2"
_Result.Add(oInfo2)

Return _Result
End Function

My windows application Call to the web service is as follow:
Private Sub CallWebservice()
Dim _Customer As New Customer.Customer
_Customer.Url = "http://localhost:1968/Customer.asmx"

Dim _Result As ArrayList
_Result = CType(_Customer.CustomersList, ArrayList)
End Sub

Thank you
 
J

John Saunders [MVP]

Sam said:
Please I need help.

I am trying to call a web service method that returns an arraylist from my
windows application but I could not do it because of the following error
message:

(value of type '1-dimensional array of object' cannot be converted to
'system.collections.ArrayList')

I need just simply a piece of sample code that show me how to return an
arraylist from a web service method and how to consume it from the client
side the right way.
....
My windows application Call to the web service is as follow:
Private Sub CallWebservice()
Dim _Customer As New Customer.Customer
_Customer.Url = "http://localhost:1968/Customer.asmx"

Dim _Result As New ArrayList()
_Result.AddRange(CType(_Customer.CustomersList, ArrayList))
End Sub
 
S

Sam

Dear John,

Thank you for your response.

Please what is the meaning of
(I could not
understand it.

Thank you.
 
J

John Saunders [MVP]

I made a slight mistake: use the following to set _Result:

Dim _Result As New ArrayList()
_Result.AddRange(_Customer.CustomersList)

This code creates a new, empty ArrayList, then uses the AddRange method of
that ArrayList to fill the ArrayList with the contents of the array that was
returned from the web service.
 
S

Sam

Dear Sir,

I added a CustomerOrders property as an arraylist to my CustomerInfo Class
which will represent the customer orders.

But I could not retrieve it from the client side and I get the same error
message that appears the first time.
(value of type '1-dimensional array of object' cannot be converted to
'system.collections.ArrayList')

Please I need your help for two things:
1. How to convert the CustomerInfo object after I added the CustomerOrders
property?
2. Which way is the efficient way to return the customers information and
their related orders from a web service method; as an arraylist of objects or
as a dataset.

'* the following is my Web service Method:
<WebMethod(), _
XmlInclude(GetType(CustomerInfo)), _
XmlInclude(GetType(Order))> _
Public Function CustomersList() As <XmlArray("CList")> ArrayList
Dim _Result As New ArrayList

'* First Customer
Dim oInfo1 As New CustomerInfo
oInfo1.CustomerNo = 1
oInfo1.CustomerName = "Customer-1"

Dim _Order1 As Order
_Order1.OrderId = 100
_Order1.OrderName = "Purchase"

oInfo1.Orders.Add(_Order1)
_Result.Add(oInfo1)

'* Second Customer
Dim oInfo2 As New CustomerInfo
oInfo2.CustomerNo = 2
oInfo2.CustomerName = "Customer-2"

Dim _Order2 As Order
_Order2.OrderId = 200
_Order2.OrderName = "Purchase"

oInfo2.Orders.Add(_Order2)
_Result.Add(oInfo2)

Return _Result
End Function

'* the following is the Call to the web service method:
Private Sub GetCustomers()
Dim _Customer As New wsCustomers.Customer
_Customer.Url = "http://dc03/CustomerServices/Customer.asmx"

Dim _Result As New ArrayList()
_Result.AddRange(_Customer.CustomersList)

Dim oInfo As wsCustomers.CustomerInfo

Dim i As Integer
For i = 0 To 3
oInfo = CType(_Result.Item(i), wsCustomers.CustomerInfo)

Dim arOrders As New ArrayList
arOrders.AddRange(oInfo_Orders)

MsgBox(oInfo.CustomerName)
Next

Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Thank you.
Sam
 
J

John Saunders [MVP]

Sam said:
Dear Sir,

I added a CustomerOrders property as an arraylist to my CustomerInfo Class
which will represent the customer orders.

But I could not retrieve it from the client side and I get the same error
message that appears the first time.
(value of type '1-dimensional array of object' cannot be converted to
'system.collections.ArrayList')

Please I need your help for two things:
1. How to convert the CustomerInfo object after I added the CustomerOrders
property?
2. Which way is the efficient way to return the customers information and
their related orders from a web service method; as an arraylist of objects
or
as a dataset.

In general, you should simply not use platform-specific types in web
services. I suggest you use an array instead.
 
S

Sam

Dear John,

Please waht about converting the CustomerInfo object back again in the
client side after I added the CustomerOrders property?

Thank you.
 
J

John Saunders [MVP]

Sam said:
Dear John,

Please waht about converting the CustomerInfo object back again in the
client side after I added the CustomerOrders property?

I'm not understanding your question, and I think you're not understanding my
answer. ; -)

I suggest that you do not use an ArrayList at all. Instead, use an array.
That way, there will be no need for any conversion. Make the CustomerOrders
property be an array, not an ArrayList. If your server-side code needs to
produce the CustomerOrders by using an ArrayList, then it can use
ArrayList.CopyTo to move the orders to the array that you will then send
back to the client. If the client needs to use an ArrayList to process the
customer orders, it can turn the array into an ArrayList once it's received
the array:

CustomerInfo cust = myService.GetCustomerInfo(customerID);
ArrayList customerOrders = new ArrayList(cust.CustomerOrders);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top