Public Class txtSurname

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim cn As OleDb.OleDbConnection
    'Dim con As OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String
    Dim DS As New DataSet
    Dim DA As OleDb.OleDbDataAdapter
    Dim sql As String

    dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
    dbSource = "Data source =C:\test2\addressBook.mdb"
    cn.ConnectionString = dbProvider & dbSource
    cn.Open()
    sql = " select * from tblcontacts"
    DA = New OleDb.OleDbDataAdapter(sql, cn)

    MsgBox("Database is now open")
    cn.Close()
    MsgBox("Database is now closed")

    DA.Fill(DS, "AddressBook")
    DA.Fill(DS, "Bacon Sandwich")
    DA = New OleDb.OleDbDataAdapter(sql, cn)
    DA.Fill(DS, "AdressBook")

    txtfirstName.Text = DS.Tables("AdressBook").Rows(0).Item(1)
    Me.txtsname.Text = DS.Tables("AdressBook").Rows(0).Item(2)

End Sub

End Class

Object reference not set to an instance of an object.
Public Class txtSurname

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cn As OleDb.OleDbConnection
        'Dim con As OleDb.OleDbConnection
        Dim dbProvider As String
        Dim dbSource As String
        Dim DS As New DataSet
        Dim DA As OleDb.OleDbDataAdapter
        Dim sql As String

        dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
        dbSource = "Data source =C:\test2\addressBook.mdb"
        cn.ConnectionString = dbProvider & dbSource
        cn.Open()
        sql = " select * from tblcontacts"
        DA = New OleDb.OleDbDataAdapter(sql, cn)

        MsgBox("Database is now open")
        cn.Close()
        MsgBox("Database is now closed")

        DA.Fill(DS, "AddressBook")
        DA.Fill(DS, "Bacon Sandwich")
        DA = New OleDb.OleDbDataAdapter(sql, cn)
        DA.Fill(DS, "AdressBook")

        txtfirstName.Text = DS.Tables("AdressBook").Rows(0).Item(1)
        Me.txtsname.Text = DS.Tables("AdressBook").Rows(0).Item(2)

    End Sub

Dani AI

Generated

A NullReferenceException means some variable is Nothing when your code tries to use it. In this thread the usual culprits are: the connection object never being instantiated, the DataSet/DataTable not containing the table or any rows, or a typo/mismatch in the table name. As pointed out, objects must be created before use, and was right to ask which exact line fails — that line tells you which variable is Nothing.

Quick checklist to find and fix the problem:

  • Put a breakpoint at the start of the Button1_Click handler and step through; inspect variables (cn, DA, DS or your DataTable) before you use them.
  • Verify the connection object is actually created and the connection string is valid before calling Open.
  • Check dataset/table names exactly (watch for spelling differences such as "AddressBook" vs "AdressBook" and odd entries like "Bacon Sandwich"). Use DS.Tables.Contains("Name") and test Rows.Count > 0 before accessing Rows(0).
  • Prefer Using blocks for connections/adapters and Fill a single DataTable if you only need the first row — it simplifies lifetime and makes it less likely something is Nothing.

Example pattern (VB.NET) you can adapt:

Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test2\addressBook.mdb;"
Dim sql As String = "SELECT * FROM tblContacts"

Using cn As New OleDb.OleDbConnection(connString)
    Using da As New OleDb.OleDbDataAdapter(sql, cn)
        Dim tbl As New DataTable()
        da.Fill(tbl)
        If tbl.Rows.Count > 0 Then
            txtFirstName.Text = tbl.Rows(0).Item("FirstName").ToString()
            txtSurname.Text   = tbl.Rows(0).Item("LastName").ToString()
        Else
            MsgBox("No records returned.")
        End If
    End Using
End Using

Extra tips: add a Try/Catch around the click handler and log ex.Message/ex.StackTrace so you can see the failing line. If you run on 64-bit Windows and get provider errors, either target x86 in project settings or use the ACE provider (Microsoft.ACE.OLEDB.12.0). Address the table-name typos and the uninitialized object first — that will almost certainly resolve the exception.

Recommended Answers

All 3 Replies

Please can anyone give me solution for this error "Object reference not set to an instance of an object.

On what line do you get this error?

You haven't created the objects. The statement

Dim cn As OleDb.OleDbConnection

says "allocate a variable named cn that can be used to access an OleDbConnection object. That statement doesn't actually create the object (it isn't pointing at anything). You need to use

Dim cn As New OleDb.OleDbConnection

This creates the object AND stores the address of it in cn. You have to do this for every object that you want to use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.