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

Quick Reply

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it has been approved by a moderator.

Name:
Email:
Verification:
Please leave this box empty:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

Shortcuts: ALT+S post or ALT+P preview