Eventually i want to load names from a database to textboxes which are created at runtime. Thought i'd try just getting names into a listbox first though as i'm new to using databases in vb and for some reason nothings getting added to the listbox

Heres the code iv'e got so far which isn't working, tried searching but can't find anything similiar using oleDb i'm guesing it's a old way of doing things or something?

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        ListBox1.Items.Clear()


        Dim connection As New OleDbConnection("provider=Microsoft.jet.Oledb.4.0;data source=P:\Comp4 project\myDB.mdb")

        Try
            connection.Open()
            Dim command As OleDbCommand = New OleDbCommand("SELECT Name FROM Component_Table WHERE Quantity<10")
            Dim sdr As OleDbDataReader
            sdr = command.ExecuteReader()






            Dim i As Integer
            i = 0


            While sdr.Read()
                ListBox1.Items.Add(sdr(i).ToString)
                i = i + 1
            End While

            sdr.Close()
            connection.Close()
        Catch ex As Exception

        End Try
    End Sub

You aren't setting the connection property of the command object to be your connection object and so your command objec can't execute. If you handled the catch block with some sort of error message you would have been informed of this. Add

command.Connection = connection

just below where you declare the command object.

Still not working, iv'e added a error message and it's not coming up so i'm guessing it might be to do with this bit?

Dim i As Integer
i = 0
While sdr.Read()
ListBox1.Items.Add(sdr(i).ToString)
i = i + 1
End While

Try this

While sdr.read()
    ListBox1.Items.Add(sdr.GetString(sdr.GetOrdinal("ColumnName")))
End While

I used GetString since we are just adding items to a Listview

doing this doesn't require the iterator "i".

also try adding

MsgBox(ex.Message)

in your catch block.

Hope this helps :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.