Search code examples
vb.netvisual-studiosqlcommanddatareader

How to navigate to next data record from database using data reader? vb.net


I would like to create a button which allow me to navigate to next data record and show in textbox.

here is my current code which can only show the first record in textbox.

    Dim cmd As New SqlCommand
    cmd.Connection = Conn
    cmd.CommandText = "SELECT sendby,enquiry,status FROM Message WHERE recipient LIKE '" & namelbl.Text & "'"

    Dim dr As SqlDataReader
    dr = cmd.ExecuteReader


    If dr.HasRows Then

        dr.Read()
        form.Label2.Text = dr.Item("sendby")
        form.Label4.Text = dr.Item("enquiry")

        form.Label6.Text = dr.Item("status")

        dr.Close()

    End If
    Conn.Close()

So how can i do to show different row data in textbox?


Solution

  • This should allow you to loop through a DataReader

    Do While dr.Read
        ' Work with items inside loop
    Loop
    If dr.IsClosed = False Then
        dr.Close()
    End If