Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SelectedValue coming up blank in listbox

Status
Not open for further replies.

vonehle

Programmer
May 16, 2006
35
US
When I run my program, it fills up the listbox properly, but when I try getting the SelectedValue, it comes back empty. I tested it by having a msgbox pop up with just the SelectedValue. Can someone tell me why the value wouldn't work?

Code:
Public Class frmCourseList

    Public strStudents As String
    Public sqlconn As New SqlConnection("Data Source = .\SQLExpress; Initial Catalog = STATEUBOOKSTORE; Integrated Security = True")

    Private Sub frmCourseList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Open the connection 
        sqlconn.Open()

        'Declare a command and assign a SQL string to it
        Dim sqlcomm As SqlCommand = sqlconn.CreateCommand()
        sqlcomm.CommandText = "SELECT * FROM Students"

        'Declare a datareader 
        Dim drd1 As SqlDataReader = sqlcomm.ExecuteReader()

        While drd1.Read()
            lstStudents.Items.Add(drd1(1).ToString & " " & drd1(2).ToString)

        End While
       
        'drd1.Close()
    End Sub
 
Nevermind. I needed SelectedItem instead.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top