Hey!

So i just started reading through some Database tuts, because i needed it for a project that i am making. I am using the HomeandLearn tutorial. So i have a database called "AdressBook" and then this code.

Public Class Form1

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Dim con As New 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:\Users\oliver\Downloads\AddressBook\AddressBook.mdb"


        con.ConnectionString = dbProvider & dbSource

        con.Open()
        
        sql = "SELECT * FROM tblContacts"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "AddressBook")


        con.Close()

        TextBox1.Text = ds.Tables("AdressBook").Rows(0).Item(1)
        TextBox2.Text = ds.Tables("AdressBook").Rows(0).Item(2)





    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

I get an error

Object reference not set to an instance of an object.

at

TextBox1.Text = ds.Tables("AdressBook").Rows(0).Item(1)

And i would probably get it at the next if it had the chance to get that far.

Any suggestions?

Thank you

Regards, Olivis.

It's telling you that you misspelled "AddressBook" as "AdressBook". And since there is no "AdressBook" there is no object to work with, thus no rows or items.

Ah, Thanks:) Man i feel like a noob.

New Error: But will post my new code:

Public Class Form1
    Dim con As New OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click


        dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
        dbSource = "Data Source = C:\Users\oliver\Downloads\AddressBook\AddressBook.mdb"


        con.ConnectionString = dbProvider & dbSource

        con.Open()
        
        sql = "SELECT * FROM tblContacts"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "AddressBook")


        con.Close()

        TextBox1.Text = ds.Tables("AddressBook").Rows(0).Item(1)
        MaskedTextBox1.Text = ds.Tables("AddressBook").Rows(0).Item(2)





    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub TextBox1_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.MouseHover
        TextBox1.Clear()
    End Sub

    Private Sub TextBox1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.MouseLeave
        TextBox1.Text = "Name, Co. or Serial"
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsNewRow As DataRow

        dsNewRow = ds.Tables("AddressBook").NewRow

        dsNewRow.Item("Serial Key") = MaskedTextBox1.Text

        ds.Tables("AddressBook").Rows.Add(dsNewRow)

        da.Update(ds, "AddressBook")

        MsgBox("New Record added to the database")




    End Sub
End Class

The error is at button4, at the first

dsnewrow = ds.Tables("AddressBook").Newrow

It gives the same

Object reference not set to an instance of an object.

as before, and can't seem to figure out why...

should be dsnewrow = ds.Tables["AddressBook").NewRow()

Actually that is how i had it at some point before, but removed it by accident or something. And it does not make a difference, sadly. (Just tried)

Member Avatar for Unhnd_Exception

Make sure your filling the data first with the load button.

At anyrate you should always check for the table.

If ds.Tables.Contains("AddressBook") Then
'Table exists and will be no object reference not set
End If

Thanks for all the answers, everything works great:)

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.