How to Consume a web service that returns an arraylist

Discussion in 'ASP .Net Web Services' started by Sam, May 28, 2007.

  1. Sam

    Sam Guest

    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
     
    Sam, May 28, 2007
    #1
    1. Advertising

  2. "Sam" <> wrote in message
    news:...
    > 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

    --
    John Saunders [MVP]
     
    John Saunders [MVP], May 29, 2007
    #2
    1. Advertising

  3. Sam

    Sam Guest

    Dear John,

    Thank you for your response.

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

    Thank you.


    "John Saunders [MVP]" wrote:

    > "Sam" <> wrote in message
    > news:...
    > > 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

    > --
    > John Saunders [MVP]
    >
    >
    >
     
    Sam, May 29, 2007
    #3
  4. 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.
    --
    John Saunders [MVP]

    "Sam" <> wrote in message
    news:...
    > Dear John,
    >
    > Thank you for your response.
    >
    > Please what is the meaning of
    > (news:...) I could not
    > understand it.
    >
    > Thank you.
    >
    >
    > "John Saunders [MVP]" wrote:
    >
    >> "Sam" <> wrote in message
    >> news:...
    >> > 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

    >> --
    >> John Saunders [MVP]
    >>
    >>
    >>
     
    John Saunders [MVP], May 29, 2007
    #4
  5. Sam

    Sam Guest

    Dear John,

    It works. Your help is highly appreciated

    Thank you.
    Sam

    "John Saunders [MVP]" wrote:

    > 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.
    > --
    > John Saunders [MVP]
    >
    > "Sam" <> wrote in message
    > news:...
    > > Dear John,
    > >
    > > Thank you for your response.
    > >
    > > Please what is the meaning of
    > > (news:...) I could not
    > > understand it.
    > >
    > > Thank you.
    > >
    > >
    > > "John Saunders [MVP]" wrote:
    > >
    > >> "Sam" <> wrote in message
    > >> news:...
    > >> > 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
    > >> --
    > >> John Saunders [MVP]
    > >>
    > >>
    > >>

    >
    >
    >
     
    Sam, May 29, 2007
    #5
  6. Sam

    Sam Guest

    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

    "Sam" wrote:

    > Dear John,
    >
    > It works. Your help is highly appreciated
    >
    > Thank you.
    > Sam
    >
    > "John Saunders [MVP]" wrote:
    >
    > > 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.
    > > --
    > > John Saunders [MVP]
    > >
    > > "Sam" <> wrote in message
    > > news:...
    > > > Dear John,
    > > >
    > > > Thank you for your response.
    > > >
    > > > Please what is the meaning of
    > > > (news:...) I could not
    > > > understand it.
    > > >
    > > > Thank you.
    > > >
    > > >
    > > > "John Saunders [MVP]" wrote:
    > > >
    > > >> "Sam" <> wrote in message
    > > >> news:...
    > > >> > 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
    > > >> --
    > > >> John Saunders [MVP]
    > > >>
    > > >>
    > > >>

    > >
    > >
    > >
     
    Sam, Jun 2, 2007
    #6
  7. "Sam" <> wrote in message
    news:...
    > 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.
    --
    John Saunders [MVP]
     
    John Saunders [MVP], Jun 2, 2007
    #7
  8. Sam

    Sam Guest

    Dear John,

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

    Thank you.

    "John Saunders [MVP]" wrote:

    > "Sam" <> wrote in message
    > news:...
    > > 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.
    > --
    > John Saunders [MVP]
    >
    >
    >
     
    Sam, Jun 3, 2007
    #8
  9. "Sam" <> wrote in message
    news:...
    > 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);
    --
    John Saunders [MVP]
     
    John Saunders [MVP], Jun 3, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. msnews.microsoft.com
    Replies:
    2
    Views:
    1,159
    =?Utf-8?B?R2FyeQ==?=
    May 19, 2004
  2. steven
    Replies:
    0
    Views:
    316
    steven
    Aug 21, 2008
  3. Christopher Ambler

    How to consume a Web Method that returns a custom class?

    Christopher Ambler, Sep 26, 2003, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    187
    Christopher Ambler
    Sep 26, 2003
  4. Timo
    Replies:
    2
    Views:
    459
  5. dgleeson422111

    consume local web service -ok consume remote service - Not OK

    dgleeson422111, Jan 26, 2010, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    1,611
    dgleeson422111
    Jan 31, 2010
Loading...

Share This Page