Share via


Value of type 'List(Of Item)' cannot be converted to 'String'.

Question

Saturday, January 30, 2016 1:35 AM

I'm new to coding in visual studio and am getting the above error.  Below is my code that is causing my error.  Can someone please tell me what I'm doing wrong?  I've bolded the line it's throwing the error on.

Imports System.Data.SqlClient
Imports System.Windows.Forms

Public Class Item
    'Public Shared dsSR As DataSet = New DataSet

    Private m_itemyear As String
    Private m_itemnumber As String
    Private m_itemdesc As Integer
    Private m_itemvalue As Decimal
    Private m_itemsoldfor As Decimal
    Private m_itempaidfor As String
    Private m_itemwinningbidder As String

    Public Property itemyear() As String
        Get
            Return m_itemyear
        End Get

        Set(ByVal value As String)
            m_itemyear = value
        End Set
    End Property

    Public Property itemnumber() As String
        Get
            Return m_itemnumber
        End Get

        Set(ByVal value As String)
            m_itemnumber = value
        End Set
    End Property

    Public Property itemdesc() As String
        Get
            Return m_itemdesc
        End Get

        Set(ByVal value As String)
            m_itemdesc = value
        End Set
    End Property

    Public Property itemvalue() As Decimal
        Get
            Return m_itemvalue
        End Get

        Set(ByVal value As Decimal)
            m_itemvalue = value
        End Set
    End Property

    Public Property itemsoldfor() As Decimal
        Get
            Return m_itemsoldfor
        End Get

        Set(ByVal value As Decimal)
            m_itemsoldfor = value
        End Set
    End Property

    Public Property itempaidfor() As String
        Get
            Return m_itempaidfor
        End Get

        Set(ByVal value As String)
            m_itempaidfor = value
        End Set
    End Property

    Public Property itemwinningbidder() As String
        Get
            Return m_itemwinningbidder
        End Get

        Set(ByVal value As String)
            m_itemwinningbidder = value
        End Set
    End Property

    Public Shared Function countkey(ByVal connstr As String, itemnum As String, bidyear As String) As Integer
        Dim selectstatement As String = "select count(*) from dbo.item where itemnumber ='" & Trim(itemnum) & "'"
        Dim connection As New SqlConnection(connstr)
        Dim reccnt As Integer = 0

        Try
            Dim command As New SqlCommand
            command.Connection = connection
            command.CommandText = selectstatement
            command.CommandType = CommandType.Text

            connection.Open()
            reccnt = command.ExecuteScalar

        Catch ex As SqlException
            ' display message box
            MessageBox.Show("Not able to connect to SQL database")

        Catch ex As Exception
            ' other exception - put something here.
            MessageBox.Show("Not able to connect to SQL database")
        Finally
            connection.Close()

        End Try

        Return reccnt

    End Function

    Public Shared Function getkey(ByVal connstr As String, itemyear As String, itemnum As String) As String
        Dim srList As New List(Of Item)
        Dim selectstatement As String = "select itemyear, itemnumber, itemdesc, itemvalue, itemsoldfor, itempaidfor, itemwinningbidder "
        selectstatement = selectstatement + "From dbo.item where itemnumber ='" & Trim(itemnum) & "' 'and itemyear='" & Trim(itemyear)
        Dim connection As New SqlConnection(connstr)
        'Dim reccnt As Integer = 0

        Try
            Dim command As New SqlCommand
            command.Connection = connection
            command.CommandText = selectstatement
            command.CommandType = CommandType.Text

            connection.Open()

            Dim reader As SqlDataReader = command.ExecuteReader()

            Do While reader.Read

                Dim sr As New Item

                sr.itemyear = reader("itemyear").ToString
                sr.itemnumber = reader("itemnumber").ToString
                sr.itemdesc = reader("itemdesc")
                sr.itemvalue = CDec(reader("itemvalue")).ToString
                sr.itemsoldfor = CDec(reader("itemsoldfor")).ToString
                sr.itempaidfor = reader("itempaidfor").ToString
                sr.itemwinningbidder = reader("itemwinningbidder").ToString

                srList.Add(sr)

            Loop

        Catch ex As SqlException
            ' display message box
            MessageBox.Show("Not able to connect to SQL database")

        Catch ex As Exception
            ' other exception - put something here.
            MessageBox.Show("Not able to connect to SQL database")
        Finally
            connection.Close()

        End Try

        Return srList

    End Function

End Class

All replies (2)

Saturday, January 30, 2016 3:29 PM ✅Answered

You may find this thread helpful:

http://stackoverflow.com/questions/13826593/cant-declare-lists-in-vb-net

BTW, as your question is specific to VB code, in future you can post where the VB experts live, here:

https://social.msdn.microsoft.com/Forums/en-US/vbgeneral/threads


Saturday, January 30, 2016 5:41 PM ✅Answered

The getkey function returns a list, however the declaration says that it returns a string. Change the definition to “Public Shared Function getkey(…) As List(Of Item)”.