0

I always get a casting error when passing data from my application to my ASMX webservice

My WebService code

   Public Function SetAlterLogTrx(ByVal qsTrx As List(Of String)) As String
    Dim oStatus As New LogAlterStatusDsp

    Dim iRec As Integer = 0
    Using DBCONN As New SqlConnection()
        Dim strConnString As String = ConfigurationManager.ConnectionStrings("Conn").ConnectionString
        DBCONN.ConnectionString = sDBConnString

        If qsTrx.Count = 0 Then
            Return "Failed"
        Else
            Dim sQueryList As Array = qsTrx.ToArray
            For Each sQuery As String In sQueryList
                Using UpdateOutCommand As New SqlCommand(sQuery, DBCONN)
                    Try
                        With DBCONN
                            .Open()
                            iRec = UpdateOutCommand.ExecuteNonQuery()
                            .Close()
                        End With
                    Catch ex As Exception
                        Return "Failed"
                    End Try
                End Using
            Next
            Return "Ok"
        End If
    End Using

End Function

My Client Code

    dim qsArray() as string
    'This array has many lines
    Using oSvc As New AnfaEngine.AnfaWSSoapClient
        Dim svcReplay As AnfaEngine.LogAlterStatusDsp
        Dim oList As New List(Of String)
        oList.AddRange(qsArray.Cast(Of String).ToList)
        svcReplay = oSvc.SetAlterLogTrx(oList)
    End Using

and I always get this error message:

Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'WS.ArrayOfString'.

What should I do to resolve this issue.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Ahmed Nazmy
  • 391
  • 1
  • 8
  • 20

2 Answers2

0

When you use "Add Service Reference" to reference the service, try using the "Advanced" tab to specify the use of a List(Of T) as the collection type.

If that doesn't work, then you'll simply have to send the service what it wants to receive.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
-1

I solved my issue in the client code as follows:

 dim qsArray() as string
'This array has many lines
Using oSvc As New AnfaEngine.AnfaWSSoapClient
    Dim svcReplay As AnfaEngine.LogAlterStatusDsp
    Dim oList As New List(Of AnfaEngine.ArrayOfString)
    oList.AddRange(qsArray.Cast(Of String).ToList)
    svcReplay = oSvc.SetAlterLogTrx(oList)
End Using
Ahmed Nazmy
  • 391
  • 1
  • 8
  • 20
  • Please consider explaining to others who land here the cause and solution, instead of just copying and pasting the working code without explanation. – Le-roy Staines Nov 04 '14 at 10:15