获取错误“表达式不会产生值

时间:2014-03-02 07:05:02

标签: vb.net

我对使用visual basic相对较新,我在从数据库填充列表框时遇到问题。出现的错误是Expression不生成值。

以下是我的表单中的代码:

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Populate Blu Ray and DVD listboxes

    Dim objMovies As New clsMovies

    objMovies.Select_BR_List()
    objMovies.Select_Dvd_List()

    For Each strBluRay As String In objMovies.Select_BR_List
        lstBluRay.Items.Add(strBluRay)
    Next

    For Each strDVD As String In objMovies.Select_Dvd_List
        lstDvd.Items.Add(strDVD)
    Next
End Sub

以下是该课程的代码:

Public Sub Select_Dvd_List()
    Dim objConnection As New SqlCeConnection(mstrCN)

    'Create SQL statement
    mstrSQL = "Select * from Dvd"
    'Instantiate command
    Dim objCommand As New SqlCeCommand(mstrSQL, objConnection)

    'open Database
    objCommand.Connection.Open()

    'Instantiate Data Reader
    Dim objDataReader As SqlCeDataReader

    'Execute SQL
    objDataReader = objCommand.ExecuteReader()

    'read Sql results
    Do While (objDataReader.Read)
        mlstDvd.Add(objDataReader.Item("dvdTitle").ToString)
    Loop

    'Close 
    objCommand.Dispose()
    objDataReader.Close()
    objDataReader.Dispose()
    objConnection.Close()
    objConnection.Dispose()
End Sub

1 个答案:

答案 0 :(得分:4)

问题在于您正在尝试枚举Sub。在VB.NET中,方法既可以是不返回值的Sub,也可以是返回值的Function。您的Select_Dvd_List方法是Sub,因此它不返回值。你有这个代码:

For Each strDVD As String In objMovies.Select_Dvd_List

这是试图循环Select_Dvd_List的结果,但正如我们已经确定的那样,Select_Dvd_List没有结果。在该方法中,您要将项目添加到mlstDvd,所以循环应该是:

For Each strDVD As String In objMovies.mlstDvd