Casting Problem returning an ArrayList

Discussion in 'ASP .Net Web Services' started by Paul D. Fox, Dec 7, 2004.

  1. Paul D. Fox

    Paul D. Fox Guest

    I am getting a casting issue when trying to get the results back from a web
    service. Its returning an ArrayList. Anyone know how to correct this?

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

    'Instantiate the Web Service
    Dim GetListOfUsersADsGroupMemberships As New
    wsActiveDirectoryWebReference.wsActiveDirectory
    Dim arrListOfUsersGroupMemberships As ArrayList
    Try

    arrListOfUsersGroupMemberships =
    GetListOfUsersADsGroupMemberships.ADsListUsersGroups(txtLoginID.Text)

    arrListOfUsersGroupMemberships.Sort()
    GenerateTable(CType(arrListOfUsersGroupMemberships, Object))
    Catch ex As Exception
    Response.Write("An exception has occurred. Please verify LoginID.")
    Finally

    End Try

    End Sub
     
    Paul D. Fox, Dec 7, 2004
    #1
    1. Advertising

  2. Paul D. Fox

    Dan Rogers Guest

    Hi Paul,

    On the calling side, assuming you used add-web-reference, there will be an
    array of some type (look at the proxy code) returned and not an array list.

    This is because on the wire you cannot distinguish the two.

    I hope this helps

    Dan Rogers
    Microsoft Corporation
    --------------------
    >From: "Paul D. Fox" <>
    >Subject: Casting Problem returning an ArrayList
    >Date: Tue, 7 Dec 2004 15:56:26 -0500
    >Lines: 26
    >X-Priority: 3
    >X-MSMail-Priority: Normal
    >X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
    >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
    >Message-ID: <#>
    >Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
    >NNTP-Posting-Host: 208.49.232.106
    >Path:

    cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09
    .phx.gbl
    >Xref: cpmsftngxa10.phx.gbl

    microsoft.public.dotnet.framework.aspnet.webservices:27080
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices
    >
    >I am getting a casting issue when trying to get the results back from a web
    >service. Its returning an ArrayList. Anyone know how to correct this?
    >
    >Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As
    >System.EventArgs) Handles btnSubmit.Click
    >
    > 'Instantiate the Web Service
    > Dim GetListOfUsersADsGroupMemberships As New
    >wsActiveDirectoryWebReference.wsActiveDirectory
    > Dim arrListOfUsersGroupMemberships As ArrayList
    >Try
    >
    > arrListOfUsersGroupMemberships =
    >GetListOfUsersADsGroupMemberships.ADsListUsersGroups(txtLoginID.Text)
    >
    > arrListOfUsersGroupMemberships.Sort()
    > GenerateTable(CType(arrListOfUsersGroupMemberships, Object))
    >Catch ex As Exception
    > Response.Write("An exception has occurred. Please verify LoginID.")
    >Finally
    >
    >End Try
    >
    >End Sub
    >
    >
    >
     
    Dan Rogers, Dec 7, 2004
    #2
    1. Advertising

  3. Paul D. Fox

    Paul D. Fox Guest

    Hmmm,

    When I look at the proxy code, it seems to be returning an "Object". Can I
    then cast it as an ArrayList at this point? Currently I get the error
    message that says "Value of type '1-dimensional array of System.Object'
    cannot be converted to 'system.Collections.ArrayList'.


    Here is my webMethod:
    '***************************************************************************
    ********
    '* Web Service Method: ADsListUsersGroups
    '* Description: This function will return the Groups a particluar AD
    User is
    '* currently a member of.
    '*
    '* Input: LoginID As String
    '* Output: ArrayList
    '***************************************************************************
    ********
    <WebMethod(Description:="This obtains a list of groups the user is a member
    of in Active Directory. Excepts LoginID As String, Returns an ArrayList of
    Groups the user is a member of.")> _
    Public Function ADsListUsersGroups(ByVal strLoginID As String) As
    ArrayList
    Dim ADsEntry1 As DirectoryEntry
    Dim arrGroupMemberships As New ArrayList
    'Dim arrGroupMemberships As Array

    Try
    ADsEntry1 = New DirectoryEntry(_DOMAIN,
    ConfigurationSettings.AppSettings("ADSI_AuthenticationID"),
    ConfigurationSettings.AppSettings("ADSI_AuthenticationPWD"))
    Dim ADsSearch As DirectorySearcher = New
    DirectorySearcher(ADsEntry1)

    ADsSearch.Filter = "(&(objectClass=user)(sAMAccountName=" &
    strLoginID & "*))"
    Dim ADsResult As SearchResult = ADsSearch.FindOne

    Dim i As Integer
    Dim iCount As Integer = ADsResult.Properties("MemberOf").Count
    If iCount > 0 Then
    'Retrieve group membership from Windows ADs and add to
    arraylist
    For i = 0 To iCount - 1
    Dim gADs As String =
    ADsResult.Properties("MemberOf").Item(i)
    Dim myGroup As String = Left(gADs, (InStr(gADs, ",") -
    1))
    arrGroupMemberships.Add(myGroup.Replace("CN=", ""))
    'arrGroupMemberships.SetValue(myGroup.Replace("CN=",
    ""), i)
    Next
    End If
    Return arrGroupMemberships
    Catch ex As Exception
    Throw New System.Exception("An exception has occurred." & vbCrLf &
    Err.Number & " - " & Err.Description)
    Finally
    End Try
    End Function

    Paul


    "Dan Rogers" <> wrote in message
    news:...
    > Hi Paul,
    >
    > On the calling side, assuming you used add-web-reference, there will be an
    > array of some type (look at the proxy code) returned and not an array

    list.
    >
    > This is because on the wire you cannot distinguish the two.
    >
    > I hope this helps
    >
    > Dan Rogers
    > Microsoft Corporation
    > --------------------
    > >From: "Paul D. Fox" <>
    > >Subject: Casting Problem returning an ArrayList
    > >Date: Tue, 7 Dec 2004 15:56:26 -0500
    > >Lines: 26
    > >X-Priority: 3
    > >X-MSMail-Priority: Normal
    > >X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
    > >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
    > >Message-ID: <#>
    > >Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
    > >NNTP-Posting-Host: 208.49.232.106
    > >Path:

    >

    cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09
    > phx.gbl
    > >Xref: cpmsftngxa10.phx.gbl

    > microsoft.public.dotnet.framework.aspnet.webservices:27080
    > >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices
    > >
    > >I am getting a casting issue when trying to get the results back from a

    web
    > >service. Its returning an ArrayList. Anyone know how to correct this?
    > >
    > >Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As
    > >System.EventArgs) Handles btnSubmit.Click
    > >
    > > 'Instantiate the Web Service
    > > Dim GetListOfUsersADsGroupMemberships As New
    > >wsActiveDirectoryWebReference.wsActiveDirectory
    > > Dim arrListOfUsersGroupMemberships As ArrayList
    > >Try
    > >
    > > arrListOfUsersGroupMemberships =
    > >GetListOfUsersADsGroupMemberships.ADsListUsersGroups(txtLoginID.Text)
    > >
    > > arrListOfUsersGroupMemberships.Sort()
    > > GenerateTable(CType(arrListOfUsersGroupMemberships, Object))
    > >Catch ex As Exception
    > > Response.Write("An exception has occurred. Please verify LoginID.")
    > >Finally
    > >
    > >End Try
    > >
    > >End Sub
    > >
    > >
    > >

    >
     
    Paul D. Fox, Dec 8, 2004
    #3
  4. Paul D. Fox

    Paul D. Fox Guest

    Yes, the proxy code says its returning an Object (When in fact the web
    service is suppose to be returning an ArrayList). In my calling code I get
    an error that says "Value of type '1-dimensional array of System.Object'
    cannot be converted to 'Systems.Collections.ArrayList'"

    Paul

    "Dan Rogers" <> wrote in message
    news:...
    > Hi Paul,
    >
    > On the calling side, assuming you used add-web-reference, there will be an
    > array of some type (look at the proxy code) returned and not an array

    list.
    >
    > This is because on the wire you cannot distinguish the two.
    >
    > I hope this helps
    >
    > Dan Rogers
    > Microsoft Corporation
    > --------------------
    > >From: "Paul D. Fox" <>
    > >Subject: Casting Problem returning an ArrayList
    > >Date: Tue, 7 Dec 2004 15:56:26 -0500
    > >Lines: 26
    > >X-Priority: 3
    > >X-MSMail-Priority: Normal
    > >X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
    > >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
    > >Message-ID: <#>
    > >Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
    > >NNTP-Posting-Host: 208.49.232.106
    > >Path:

    >

    cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09
    > phx.gbl
    > >Xref: cpmsftngxa10.phx.gbl

    > microsoft.public.dotnet.framework.aspnet.webservices:27080
    > >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices
    > >
    > >I am getting a casting issue when trying to get the results back from a

    web
    > >service. Its returning an ArrayList. Anyone know how to correct this?
    > >
    > >Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As
    > >System.EventArgs) Handles btnSubmit.Click
    > >
    > > 'Instantiate the Web Service
    > > Dim GetListOfUsersADsGroupMemberships As New
    > >wsActiveDirectoryWebReference.wsActiveDirectory
    > > Dim arrListOfUsersGroupMemberships As ArrayList
    > >Try
    > >
    > > arrListOfUsersGroupMemberships =
    > >GetListOfUsersADsGroupMemberships.ADsListUsersGroups(txtLoginID.Text)
    > >
    > > arrListOfUsersGroupMemberships.Sort()
    > > GenerateTable(CType(arrListOfUsersGroupMemberships, Object))
    > >Catch ex As Exception
    > > Response.Write("An exception has occurred. Please verify LoginID.")
    > >Finally
    > >
    > >End Try
    > >
    > >End Sub
    > >
    > >
    > >

    >
     
    Paul D. Fox, Dec 8, 2004
    #4
  5. Paul D. Fox

    Paul D. Fox Guest

    Found the issue, although I don't quite understand why since an ArrayList is serializable. What I had to do was to cast the ArrayList in the WebService's return to an Array. See the code below
    '***********************************************************************************
    '* Web Service Method: ADsListUsersGroups
    '* Description: This function will return the Groups a particluar AD User is
    '* currently a member of.
    '*
    '* Input: LoginID As String
    '* Output: ArrayList
    '***********************************************************************************

    <WebMethod(Description:="This obtains a list of groups the user is a member of in Active Directory. Excepts LoginID As String, Returns an ArrayList of Groups the user is a member of.")> _

    Public Function ADsListUsersGroups(ByVal strLoginID As String) As Array

    Dim ADsEntry1 As DirectoryEntry
    Dim arrGroupMemberships As New ArrayList

    Try

    ADsEntry1 = New DirectoryEntry(_DOMAIN, ConfigurationSettings.AppSettings("ADSI_AuthenticationID"), ConfigurationSettings.AppSettings("ADSI_AuthenticationPWD"))
    Dim ADsSearch As DirectorySearcher = New DirectorySearcher(ADsEntry1)
    ADsSearch.Filter = "(&(objectClass=user)(sAMAccountName=" & strLoginID & "*))"

    Dim ADsResult As SearchResult = ADsSearch.FindOne
    Dim i As Integer
    Dim iCount As Integer = ADsResult.Properties("MemberOf").Count

    If iCount > 0 Then
    'Retrieve group membership from Windows ADs and add to arraylist
    For i = 0 To iCount - 1
    Dim gADs As String = ADsResult.Properties("MemberOf").Item(i)
    Dim myGroup As String = Left(gADs, (InStr(gADs, ",") - 1))

    arrGroupMemberships.Add(myGroup.Replace("CN=", ""))
    Next
    End If

    Return arrGroupMemberships.ToArray

    Catch ex As Exception
    Throw New System.Exception("An exception has occurred." & vbCrLf & Err.Number & " - " & Err.Description)
    Finally

    End Try

    End Function



    Paul

    "Paul D. Fox" <> wrote in message news:...
    > Yes, the proxy code says its returning an Object (When in fact the web
    > service is suppose to be returning an ArrayList). In my calling code I get
    > an error that says "Value of type '1-dimensional array of System.Object'
    > cannot be converted to 'Systems.Collections.ArrayList'"
    >
    > Paul
    >
    > "Dan Rogers" <> wrote in message
    > news:...
    > > Hi Paul,
    > >
    > > On the calling side, assuming you used add-web-reference, there will be an
    > > array of some type (look at the proxy code) returned and not an array

    > list.
    > >
    > > This is because on the wire you cannot distinguish the two.
    > >
    > > I hope this helps
    > >
    > > Dan Rogers
    > > Microsoft Corporation
    > > --------------------
    > > >From: "Paul D. Fox" <>
    > > >Subject: Casting Problem returning an ArrayList
    > > >Date: Tue, 7 Dec 2004 15:56:26 -0500
    > > >Lines: 26
    > > >X-Priority: 3
    > > >X-MSMail-Priority: Normal
    > > >X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
    > > >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
    > > >Message-ID: <#>
    > > >Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
    > > >NNTP-Posting-Host: 208.49.232.106
    > > >Path:

    > >

    > cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09
    > > phx.gbl
    > > >Xref: cpmsftngxa10.phx.gbl

    > > microsoft.public.dotnet.framework.aspnet.webservices:27080
    > > >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices
    > > >
    > > >I am getting a casting issue when trying to get the results back from a

    > web
    > > >service. Its returning an ArrayList. Anyone know how to correct this?
    > > >
    > > >Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As
    > > >System.EventArgs) Handles btnSubmit.Click
    > > >
    > > > 'Instantiate the Web Service
    > > > Dim GetListOfUsersADsGroupMemberships As New
    > > >wsActiveDirectoryWebReference.wsActiveDirectory
    > > > Dim arrListOfUsersGroupMemberships As ArrayList
    > > >Try
    > > >
    > > > arrListOfUsersGroupMemberships =
    > > >GetListOfUsersADsGroupMemberships.ADsListUsersGroups(txtLoginID.Text)
    > > >
    > > > arrListOfUsersGroupMemberships.Sort()
    > > > GenerateTable(CType(arrListOfUsersGroupMemberships, Object))
    > > >Catch ex As Exception
    > > > Response.Write("An exception has occurred. Please verify LoginID.")
    > > >Finally
    > > >
    > > >End Try
    > > >
    > > >End Sub
    > > >
    > > >
    > > >

    > >

    >
    >
     
    Paul D. Fox, Dec 10, 2004
    #5
  6. Paul D. Fox

    Dan Rogers Guest

    Great.

    You need to remember that the type is not what comes over the wire. It is
    the representation of the repeating data that has every appearance of
    array. there are ways to make your proxy programming model use array
    lists, but this calls for a container around your repeating elements being
    represented on the wire.

    Dan
    --------------------
    >From: "Paul D. Fox" <>
    >References: <#>

    <>
    <>
    >Subject: Re: Casting Problem returning an ArrayList
    >Date: Fri, 10 Dec 2004 09:50:01 -0500
    >Lines: 349
    >Organization: Ascii Technologies
    >MIME-Version: 1.0
    >Content-Type: multipart/alternative;
    > boundary="----=_NextPart_000_0009_01C4DE9D.9E38C990"
    >X-Priority: 3
    >X-MSMail-Priority: Normal
    >X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
    >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
    >Message-ID: <O$>
    >Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
    >NNTP-Posting-Host: 208.49.232.75
    >Path:

    cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!tornado.fastwebnet.it!tiscali!new
    sfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!t-online.de!TK2MSFTNGP08.ph
    x.gbl!tk2msftngp13.phx.gbl
    >Xref: cpmsftngxa10.phx.gbl

    microsoft.public.dotnet.framework.aspnet.webservices:27128
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices
    >
    >Found the issue, although I don't quite understand why since an ArrayList

    is serializable. What I had to do was to cast the ArrayList in the
    WebService's return to an Array. See the code below
    >'**************************************************************************

    *********
    >'* Web Service Method: ADsListUsersGroups
    >'* Description: This function will return the Groups a particluar AD User

    is
    >'* currently a member of.
    >'*
    >'* Input: LoginID As String
    >'* Output: ArrayList
    >'**************************************************************************

    *********
    ><WebMethod(Description:="This obtains a list of groups the user is a

    member of in Active Directory. Excepts LoginID As String, Returns an
    ArrayList of Groups the user is a member of.")> _
    >Public Function ADsListUsersGroups(ByVal strLoginID As String) As Array
    >Dim ADsEntry1 As DirectoryEntry
    >Dim arrGroupMemberships As New ArrayList
    >Try
    >ADsEntry1 = New DirectoryEntry(_DOMAIN,

    ConfigurationSettings.AppSettings("ADSI_AuthenticationID"),
    ConfigurationSettings.AppSettings("ADSI_AuthenticationPWD"))
    >Dim ADsSearch As DirectorySearcher = New DirectorySearcher(ADsEntry1)
    >ADsSearch.Filter = "(&(objectClass=user)(sAMAccountName=" & strLoginID &

    "*))"
    >Dim ADsResult As SearchResult = ADsSearch.FindOne
    >Dim i As Integer
    >Dim iCount As Integer = ADsResult.Properties("MemberOf").Count
    >If iCount > 0 Then
    > 'Retrieve group membership from Windows ADs and add to arraylist
    > For i = 0 To iCount - 1
    > Dim gADs As String = ADsResult.Properties("MemberOf").Item(i)
    > Dim myGroup As String = Left(gADs, (InStr(gADs, ",") - 1))
    >
    > arrGroupMemberships.Add(myGroup.Replace("CN=", ""))
    > Next
    >End If
    >Return arrGroupMemberships.ToArray
    >Catch ex As Exception
    > Throw New System.Exception("An exception has occurred." & vbCrLf &

    Err.Number & " - " & Err.Description)
    >Finally
    >End Try
    >End Function
    >Paul
    >"Paul D. Fox" <> wrote in message

    news:...
    >> Yes, the proxy code says its returning an Object (When in fact the web
    >> service is suppose to be returning an ArrayList). In my calling code I

    get
    >> an error that says "Value of type '1-dimensional array of System.Object'
    >> cannot be converted to 'Systems.Collections.ArrayList'"
    >>
    >> Paul
    >>
    >> "Dan Rogers" <> wrote in message
    >> news:...
    >> > Hi Paul,
    >> >
    >> > On the calling side, assuming you used add-web-reference, there will

    be an
    >> > array of some type (look at the proxy code) returned and not an array

    >> list.
    >> >
    >> > This is because on the wire you cannot distinguish the two.
    >> >
    >> > I hope this helps
    >> >
    >> > Dan Rogers
    >> > Microsoft Corporation
    >> > --------------------
    >> > >From: "Paul D. Fox" <>
    >> > >Subject: Casting Problem returning an ArrayList
    >> > >Date: Tue, 7 Dec 2004 15:56:26 -0500
    >> > >Lines: 26
    >> > >X-Priority: 3
    >> > >X-MSMail-Priority: Normal
    >> > >X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
    >> > >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
    >> > >Message-ID: <#>
    >> > >Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
    >> > >NNTP-Posting-Host: 208.49.232.106
    >> > >Path:
    >> >

    >>

    cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09
    >> > phx.gbl
    >> > >Xref: cpmsftngxa10.phx.gbl
    >> > microsoft.public.dotnet.framework.aspnet.webservices:27080
    >> > >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices
    >> > >
    >> > >I am getting a casting issue when trying to get the results back from

    a
    >> web
    >> > >service. Its returning an ArrayList. Anyone know how to correct

    this?
    >> > >
    >> > >Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As
    >> > >System.EventArgs) Handles btnSubmit.Click
    >> > >
    >> > > 'Instantiate the Web Service
    >> > > Dim GetListOfUsersADsGroupMemberships As New
    >> > >wsActiveDirectoryWebReference.wsActiveDirectory
    >> > > Dim arrListOfUsersGroupMemberships As ArrayList
    >> > >Try
    >> > >
    >> > > arrListOfUsersGroupMemberships =
    >> > >GetListOfUsersADsGroupMemberships.ADsListUsersGroups(txtLoginID.Text)
    >> > >
    >> > > arrListOfUsersGroupMemberships.Sort()
    >> > > GenerateTable(CType(arrListOfUsersGroupMemberships, Object))
    >> > >Catch ex As Exception
    >> > > Response.Write("An exception has occurred. Please verify

    LoginID.")
    >> > >Finally
    >> > >
    >> > >End Try
    >> > >
    >> > >End Sub
    >> > >
    >> > >
    >> > >
    >> >

    >>
    >>

    >
     
    Dan Rogers, Dec 10, 2004
    #6
    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. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,771
    Natty Gur
    Aug 19, 2003
  2. Kaidi
    Replies:
    4
    Views:
    2,428
    Kaidi
    Jan 3, 2004
  3. xz
    Replies:
    16
    Views:
    2,405
  4. Philipp
    Replies:
    6
    Views:
    940
    Arne Vajhøj
    May 28, 2008
  5. Wally Barnes
    Replies:
    3
    Views:
    533
    Wally Barnes
    Nov 20, 2008
Loading...

Share This Page