Start New Discussion within our Software Development Community

Please this is my code to display records from my ms-access database whenever i click the button next. But the code is only displaying the last record in the database. Please HELP is needed. I have tried using for loop but it is giving me error.

Imports System.Data.OleDb
Public Class Form4
    Dim cn As OleDbConnection
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As _
    System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
    System.EventArgs) Handles Button1.Click
        Try

            cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Chukwuma Peter\My Documents\Visual Studio 2005\Projects\Peterson Document Management System\Peter_File_system.mdb")
            'provider to be used when working with access database
            cn.Open()
            cmd = New OleDbCommand("select * from IncomingFile", cn)
            dr = cmd.ExecuteReader
            While dr.Read()

                TextBox1.Text = dr(0)
                TextBox2.Text = dr(1)
                TextBox3.Text = dr(2)
                TextBox4.Text = dr(3)
                TextBox5.Text = dr(4)
                TextBox6.Text = dr(5)
                TextBox7.Text = dr(6)
                TextBox8.Text = dr(7)
                ' loading data into TextBoxes by column index
            End While

        Catch
        End Try
        dr.Close()
        cn.Close()
    End Sub
End Class

>whenever i click the button next. But the code is only displaying the last record in the database.

It will not work. DataReader instance reads a forward-only, read-only stream of data from a data source.

Use DataSet (Relational classes) and DataAdapter.

This article has been dead for over six months. Start a new discussion instead.