Invalid Cast Exception when consuming web method

DotNetNooby
DotNetNooby used Ask the Experts™
on
When trying to consume a method from the web service I get an InvalidCastException - Unable to cast object of type 'System.String' to type 'System.String[]'

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Property tagIDs() As ArrayList
        Get
            If ViewState("tagIDs") Is Nothing Then
                ViewState("tagIDs") = New ArrayList
            End If
            Return CType(ViewState("tagIDs"), ArrayList)
        End Get
        Set(ByVal value As ArrayList)
            ViewState("tagIDs") = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        lblDisplay.Text += txtID.Text & " "
        tagIDs.Add(txtID.Text)

        txtID.Text = ""

    End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        'submit array to web service
        ViewState("studentTag") = txtStudentTag.Text

        Dim ws As New lms_reference.LMS_Interface
        ws.Credentials = New NetworkCredential("UserID", "Password")
        ws.SetStudentTags(ViewState("studentTag"), ViewState("tagIDs"))

    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bob HoffmanDeveloper

Commented:
whats ViewState("tagIDs") look like? Is it an array or a string where the elements are separated by a comma. You may need to bring in as a string then split it into your array.

Author

Commented:
It will be a 4 digit number e.g.(1366).  It could be a single entry or an array of entries.
Bob HoffmanDeveloper

Commented:
try setting Property tagIDs up as a string array instead of an ArrayList
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Author

Commented:
When I use Array instead of ArrayList in my tagIDs property it doesn't like the if statement
If ViewState("tagIDs") Is Nothing Then
                ViewState("tagIDs") = New Array
            End If

Gives me 'New' cannot be used on a class that is declared 'MustInherit' on the "New Array"
    Protected Property tagIDs() As Array
        Get
            If ViewState("tagIDs") Is Nothing Then
                ViewState("tagIDs") = New Array
            End If
            Return CType(ViewState("tagIDs"), Array)
        End Get
        Set(ByVal value As Array)
            ViewState("tagIDs") = value
        End Set
    End Property

Open in new window

Developer
Commented:
Try this

   Protected Property tagIDs() As String()
        Get
            If ViewState("tagIDs") Is Nothing Then
                ViewState("tagIDs") = New String()
            End If
            Return CType(ViewState("tagIDs"), String())
        End Get
        Set(ByVal value As String())
            ViewState("tagIDs") = value
        End Set
    End Property

Open in new window

Author

Commented:
This Works

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial