Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Iam new to vb.net programming.I want to add records to an access database table. I get the error message "NullReferenceException was unhandled" when using the following code.Can anyone
help to correct this.


VB
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
        Dim inc As Integer
        dbProvider = "provider=microsoft.ACE.OLEDB.12.0;"
        dbSource = "DATA SOURCE=C:\cwhData\cwhData.accdb"
        con.ConnectionString = dbProvider & dbSource
        con.Open()
        sql = "select * from customer"
        da = New OleDb.OleDbDataAdapter(sql, con)
        If inc <> -1 Then
            Dim cb As New OleDb.OleDbCommandBuilder(da)

            Dim dsNewRow As DataRow
            dsNewRow = ds.Tables("customer").NewRow()
            dsNewRow.Item("CUSTOMERID") = txtCustomerID.Text
            dsNewRow.Item("customername") = txtCustomerName.Text
            ds.Tables("customer").Rows.Add(dsNewRow)
            da.Update(ds, "customer")
            MsgBox("New record added to the database")



        End If
        MsgBox("database is now open")
        con.Close()
        MsgBox("database is now closed")
    End Sub
Posted
Updated 18-Dec-13 2:41am
v2
Comments
Pheonyx 18-Dec-13 8:42am    
Have you stepped through the code?
While line is the error occurring on?
You have no error handling in this code, so it will not fail gracefully.
Estys 18-Dec-13 8:42am    
Where's the error? Have you tried stepping through the code with the debugger?
thatraja 18-Dec-13 9:03am    
Agree with Pheonyx. While line is the error occurring on?

1 solution

Start here: Accessing Microsoft Office Data from .NET Applications[^]

More examples, you'll find here: CP Knowledge Base[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Dec-13 9:06am    
5ed. OP re-posted the question.
—SA
Maciej Los 18-Dec-13 9:10am    
Thank you.
His question is gone ;)

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