Since you haven't declared it within the method, it seems you're storing the database connection object
con
in a field of your class, and reusing it in different methods. That's a really bad idea. Instead, you should create it at the point it's required, and wrap it in a
Using
block to ensure it's always disposed of properly.
There's also no need to open and close the connection when you're using a
DataAdapter
- the adapter will take care of that for you.
Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click
Try
Dim ds As New Database1DataSet25
Using con As New OleDb.OleDbConnection("... YOUR CONNECTION STRING HERE ...")
Using cmd As New OleDb.OleDbCommand("SELECT book_ID, title,Authors,published_year,Quantity,ISBN from books", con)
Dim da As New OleDb.OleDbDataAdapter(cmd)
da.Fill(ds)
End Using
End Using
Dim books As DataTable = ds.Tables("books")
Dim MaxRows As Integer = books.Rows.Count
inc = -1
If inc <> MaxRows - 1 Then
inc = inc + 1
Dim row As DataRow = books.Rows(inc)
Book_IDTextBox.Text = row.Item("book_ID").ToString
TitleTextBox.Text = row.Item("title").ToString
AuthorsTextBox.Text = row.Item("Authors").ToString
Published_yearTextBox.Text = row.Item("published_year").ToString
QuantityTextBox.Text = row.Item("Quantity").ToString
ISBNTextBox.Text = row.Item("ISBN").ToString
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub