Problem Assigning value from array to textbox

Posted on 2008-11-13
Last Modified: 2012-06-27
I'm getting the error message when I use the dropdown box to pass a value to the database thru a stored proc and return another value.  Through debugging I've determined that the array is indeed getting filled with one value, but when I try to place this value in the textbox I get the error.  Also, when I removed the val() from around the array variable name and just had the .ToString() there, it did not error but passed the text 'Ticket' to the text box, which is the class name?!?

Thanks a bunch for any help,

Argument 'Expression' cannot be converted to type 'Ticket'. 

Exception Details: System.ArgumentException: Argument 'Expression' cannot be converted to type 'Ticket'.


Line 23:      arTx = GetIT.GetTickets(strCustName)

Line 24: 

Line 25(KABOOM):  Me.TicketNo.Text = Val(arTx(0)).ToString()  

Here is entire function:

    Private Sub GetEmployeesTickets(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustNames.SelectedIndexChanged

        Dim GetIT As New DOTIT

        Dim strCustName As String

        Dim arTx As ArrayList

        'strCustName = Val(Me.CustNames.SelectedItem)

        If Page.IsPostBack Then

            strCustName = CustNames.SelectedItem.ToString()

            arTx = GetIT.GetTickets(strCustName)

            Me.TicketNo.Text = Val(arTx(0)).ToString() 

        End If

    End Sub

'Function being called:

Public Function GetTickets(ByVal strCustID As String) As ArrayList

        'pg 271

        Dim connectionString As String = ConfigurationManager.ConnectionStrings("DOTITConnectionString").ConnectionString()

        Dim con As New SqlConnection(connectionString)

        Dim cmd As New SqlCommand("pr_GetTickets", con)

        cmd.CommandType = CommandType.StoredProcedure

        cmd.Parameters.Add(New SqlParameter("@CustID", SqlDbType.VarChar))

        cmd.Parameters("@CustID").Value = strCustID

        Dim arTickets As New ArrayList


        Dim reader As SqlDataReader = cmd.ExecuteReader()

        Do While reader.Read()

              Dim Ticks As New Ticket(reader("TicketID")) 





        Return arTickets

    End Function

End Class

'Ticket class:

Public Class Ticket

    Dim intTicketID As Integer

    Public Sub New(ByVal intTicketID As Integer)                 Me.TicketID = intTicketID

    End Sub

    Public Property TicketID() As Integer


            Return intTicketID

        End Get

        Set(ByVal value As Integer)

            intTicketID = value

        End Set

    End Property

End Class

Open in new window

Question by:MoonDoggie
    LVL 14

    Expert Comment

    Does it make a difference on the line that blows up if you actually reference the TicketId property?   Maybe something like   Val(arTx(0).TicketId).ToString().  This may not work unless you CType the array item  like   Val(CType(arTx(0),Ticket).TicketId).  

    Another possiblity would be in your Ticket class to override the ToString method to return the TicketId.   Then instead of it returning the word Ticket, it would return the TicketId instead.
    LVL 14

    Accepted Solution

    Me.TicketNo.Text = CType(arTx(0), Ticket).TicketID.ToString()

    insteed of

     Me.TicketNo.Text = Val(arTx(0)).ToString()

    You getting error bcoz ... val() function returns decimal type so this need correct Argument
    LVL 1

    Author Closing Comment

    Perfect, thanks!
    LVL 1

    Author Comment

    So I'm trying to understand exactly what is going on here.  Am I passing the class object Ticket within an array then grabbing the property TicketID out of it?  If so is this the cleanest way to do it or should I be just passing the integer value of TicketID in the array?  Thanks.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now