passing an arrayList to web service

G

Guest

I have a following structure that I am using with array list:
Private Structure arrayliststruct
Public Name As String
Public value As String
Public type As String
End Structure

and following function to format arraylist the way I want and calling it
from button click event:
Private Function formatarray(ByVal inarray As ArrayList, _
ByVal inName As String, _
ByVal initem As String, _
ByVal initemtype As String)

Dim arrayparam As arrayliststruct

arrayparam.Name = inName
arrayparam.value = initem
arrayparam.type = initemtype
inarray.Add(arrayparam)

Return inarray

End Function

I have follwoing code in button click event

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSubmit.Click

Dim inarray As New ArrayList
inarray = formatarray(inarray, "Email", "(e-mail address removed)", "String")
inarray = formatarray(inarray, "Name", "My Name", "String")

' then I want to pass this code to web serivce that accepts arrary list
Dim postEv As New testev.wsTEST
postEv.Credentials = Net.CredentialCache.DefaultCredentials
postEv.PreAuthenticate = True
postEv.PostExtEvent("Test", "Source", inarray(2))

End Sub

I don't know how to pass arraylist to a webservice.
please help:
 
G

Guest

I don't want to pass arralylist index with the arrary list and I am getting
follwoing errors
1> if passed without index
"number of indices is less than the number of dimensions"

2> if passed with index
Index was out of range. Must be non-negative and less than the size of the
collection.
Parameter name: index
 
J

JosephByrns

The function:
Private Function formatarray(ByVal inarray As ArrayList, _
ByVal inName As String, _
ByVal initem As String, _
ByVal initemtype As String)

Isn't declared as anything, presumably you want:

Private Function formatarray(ByVal inarray As ArrayList, _
ByVal inName As String, _
ByVal initem As String, _
ByVal initemtype As String) As
ArrayList

Don't know if that is a typo or if it will fix your problem. If it does not
fix the problem you might like to post the declaration for the webservice
function also.
 
G

Guest

Thnaks Joseph

Thanks for reply..

I changed the function and declared as
Private Function formatarray(ByVal inarray As ArrayList, _
ByVal inName As String, _
ByVal initem As String, _
ByVal initemtype As String) As ArrayList

Stil the same problem..

my web service is

Public Sub PostExtEvent(ByVal EventName As String, ByVal ExternalSource As
String, ByVal inparam As ArrayList)

Thanks
 
G

Guest

I found some info on:
http://support.microsoft.com/default.aspx/kb/814320

and changed inarray(2) to inarray(0)

my web service is
Public Sub PostExtEvent(ByVal EventName As String, ByVal ExternalSource As
String, ByVal inparam As ArrayList)

this is how I see it when try to call
PostExtEvent(EventName as string, ExternalSource as string, inparam() as
object

and I get error message as:
Unable to cast object of type 'arrayliststruct' to type 'System.Object[]'.

Thanks//
 
J

Joseph Byrns

You should just be calling it like:

postEv.PostExtEvent("Test", "Source", inarray)

without the brackets or index, does it still not work then?
 
G

Guest

Hi Joseph,

Thanks Again..

That only works if I move my function from webservice to local program...
I I do the same with webservice
Value of type 'System.Collections.ArrayList' cannot be converted to
'1-dimensional array of Object'
I donot understand why webservice is not expecting arraylist but the
OBJECT...

PostExtEvent(EventName as string, ExternalSource as string, inparam() as
OBJECT

event if I declared as an arraylist
 
J

JosephByrns

Unfortunately I don't know what is causing your difficulties. Included
below is an example I have working here (excluding all the auto generated
stuff).

Note that in your example you have the arrayliststruct declared in the aspx
part of the application, you really need to declare in the webservice part.

This part is the Webservice:

Imports System.Web.Services
Imports System.Xml.Serialization

Public Class myService
Inherits System.Web.Services.WebService
<WebMethod(), XmlInclude(GetType(arrayliststruct))> _
Public Sub PostExtEvent(ByVal EventName As String, ByVal ExternalSource As
String, ByVal inparam As Object())
For Each obj As Object In inparam
Dim als As arrayliststruct = CType(obj, arrayliststruct)
Dim n As String = als.Name
Dim v As String = als.value
Dim t As String = als.type
Next
End Sub

Public Structure arrayliststruct
Public Name As String
Public value As String
Public type As String
End Structure

End Class

And this part is the web form part of the code:

Public Class WebForm1
Inherits System.Web.UI.Page

Private Function formatarray(ByVal inarray As ArrayList, _
ByVal inName As String, _
ByVal initem As String, _

ByVal initemtype As String) As ArrayList

Dim arrayparam As New myExampleService.arrayliststruct
arrayparam.Name = inName
arrayparam.value = initem
arrayparam.type = initemtype
inarray.Add(arrayparam)

Return inarray
End Function

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSubmit.Click
Dim inarray As New ArrayList
inarray = formatarray(inarray, "Email", "(e-mail address removed)", "String")
inarray = formatarray(inarray, "Name", "My Name", "String")

Dim ws As New myExampleService.myService
ws.PostExtEvent("Test", "Source", inarray.ToArray)
End Sub

End Class
 
G

Guest

Thanks Joseph,

Thant is excellent suggession and I am working on that...
However, I get follwoing error when trying to pass object to web service..

There was an error generating the XML document.
"The type System.Object[] may not be used in this context."

Thank you.
 

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

Top