Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi there !
Im new to the vb.net and trying to connect next button to the record set connected via access. but it shows an error code as "object reference not set to an instance of an object".
I tried to fix in many ways but it cannot find the bug. can anybody can help me to sort this error pls.

Many Thanks

What I have tried:

VB.NET
Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click

        Try

            Dim sql As String
            Dim cmd As New OleDb.OleDbCommand
            Dim dt As New DataTable
            Dim da As New OleDb.OleDbDataAdapter
            Dim ds As New Database1DataSet25
            Dim MaxRows As Integer


            con.Open()
            sql = "SELECT book_ID, title,Authors,published_year,Quantity,ISBN from books"

            cmd.Connection = con
            cmd.CommandText = sql
            da.SelectCommand = cmd
            da.Fill(ds)

            MaxRows = ds.Tables("book_ID").Rows.Count
            inc = -1
            con.Close()
            If inc <> MaxRows - 1 Then
                inc = inc + 1


                Book_IDTextBox.Text=ds.Tables("books").Rows(inc).Item("book_ID").ToString
TitleTextBox.Text = ds.Tables("books").Rows(inc).Item("title").ToString
AuthorsTextBox.Text = ds.Tables("books").Rows(inc).Item("Authors").ToString             
Published_yearTextBox.Text=ds.Tables("books").Rows(inc).Item("published_year").ToString
QuantityTextBox.Text = ds.Tables("books").Rows(inc).Item("Quantity").ToString
ISBNTextBox.Text = ds.Tables("books").Rows(inc).Item("ISBN").ToString

            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
       
        End Try



    End Sub
End Class
Posted
Updated 9-Mar-23 23:21pm
v2
Comments
CHill60 10-Mar-23 4:44am    
When you debug which line is throwing the exception?
Where is con defined and initialised?

Edit: You might also find this useful: What is Debugging, How to Debug… A Beginners Guide[^]

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.
VB.NET
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
 
Share this answer
 
Unfortunately you did not tell us at which line the error occurs.

My recommendation: Please learn debugging!

An introduction to this can be found here:
First look at the debugger - Visual Studio (Windows) | Microsoft Learn[^]
 
Share this answer
 
Comments
Member 15947161 10-Mar-23 4:51am    
The error show in connection and data adapter line
This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900