I am relatively new to using visual basic and I am having a problem populating a list box from a database. The error that comes up is Expression does not produce a value.
Here is the code from My form:
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
And here's the code from the class:
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
The issue is that you're trying to enumerate a Sub. In VB.NET, methods can either a Sub, which doesn't return a value, or a Function, which does return a value. Your Select_Dvd_List method is a Sub, so it doesn't return a value. You have this code though:
For Each strDVD As String In objMovies.Select_Dvd_List
That is trying to loop through the result of Select_Dvd_List but, as we've already established, Select_Dvd_List has no result. In that method, you are adding items to mlstDvd so surely that loop should be:
For Each strDVD As String In objMovies.mlstDvd
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With