News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

ListBox from a database query in VB .NET

Started by nandagopal, May 20, 2009, 07:14 PM

Previous topic - Next topic

nandagopal

Execute the query. Use a data reader object to loop through the results, adding the fields' values to the ListBox separated by tab characters.


Private Sub LoadListBoxFromQuery(ByVal lst As ListBox, _
    ByVal query As String)
    lst.Items.Clear()

    ' Open the connection.
    connUsers.Open()

    ' Make a SELECT Command.
    Dim cmd As New OleDb.OleDbCommand( _
        query, connUsers)

    ' Execute the query.
    Dim db_reader As OleDbDataReader = _
        cmd.ExecuteReader(CommandBehavior.Default)

    ' Display the results.
    Dim txt As String
    Dim i As Integer
    Do While db_reader.Read
        txt = db_reader.Item(0).ToString
        For i = 1 To db_reader.FieldCount - 1
            txt &= vbTab & db_reader.Item(i).ToString
        Next i
        lst.Items.Add(txt)
    Loop

    ' Close the connection.
    connUsers.Close()
End Sub



pradeep prem

it can easily recognize this program from this site

it has given great explanation to solve