Click here to Skip to main content
15,042,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm learning how to add data into ACCESS database. My code generates error when I run it. The error is "Object reference not set to an instance of an object." Here is the code:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim strSQL As String = "SELECT * FROM tbltutorial"
    Dim dtAdapter As OleDbDataAdapter = New OleDbDataAdapter(strSQL, Koneksi)
    Dim dtSet As DataSet = New DataSet
    Dim dtTable As DataTable = New DataTable
    Dim dtRow As DataRow
    dtTable = dtSet.Tables("tbltutorial")
    dtRow = dtTable.NewRow() '<----------------<-HERE IS WHERE THE ERROR OCCURS
    dtRow("kode") = Me.T1.Text
    dtRow("nama") = Me.T2.Text
    dtRow("alamat") = Me.T3.Text
    dtRow("tanggal") = Me.T4.Text
    dtRow("setoran") = Me.T5.Text
    dtRow("penarikan") = Me.T6.Text
    dtRow("saldo") = Me.T7.Text

End Sub

Please help me how to solve the problem?

1 solution

I would suggest that the DataTable dtTable is being set to nothing as the line dtTable = dtSet.Tables("tbltutorial") didn't find any tables called tbltutorial in the DataSet dtSet

I would break point your code and inspect the collection of tables in your DataSet to test this.
adadero 13-Jul-11 6:16am
I'm confused. I'm newbie. Would you please give me the correct example? I mean my corrected code.
Dave Kreskowiak 13-Jul-11 12:39pm
Except for checking if dtRow is Nothing before you try and use it, your code is correct. The problem is that you're trying to find a DataTable object called "tbltutorial" in your DataSet and it doesn't exist.

Its possible that your DataTable objects in your DataSet do not have names, so you'd have to get at them using dtSet.Tables(numericIndex) instead.
adadero 13-Jul-11 13:45pm
It seems to be OK but the error occurs here --> dtAdapter.Update(dtSet). "Update requires a valid InsertCommand when passed DataRow collection with new rows."

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