Hi. I am new to this forum, but i have been playing with Visual Basic since 4.0.

Anyway here is my problem. I am getting error and can't figure out why.

Dim connectionString As String
        Dim cnn As OleDbConnection
        connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Database\tmprcStavka.mdb;"
        cnn = New OleDbConnection(connectionString)
        Try
            cnn.Open()
            MsgBox("Connection Open ! ")
            
            Dim ds As New DataSet
            Dim da As OleDb.OleDbDataAdapter
            Dim Sql As String = "INSERT INTO RacuniStavke"

            da = New OleDb.OleDbDataAdapter(Sql, cnn)
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            cntx = 0
            
            Do Until cntx = dbtStavke.RowCount - 1

                ArtikalIznos = dbtStavke.Rows(cntx).Cells("IznosDataGridViewTextBoxColumn").Value()
                ArtikalNaziv = dbtStavke.Rows(cntx).Cells("ArtikalDataGridViewTextBoxColumn").Value()
                ArtikalJMJ = dbtStavke.Rows(cntx).Cells("JMJDataGridViewTextBoxColumn").Value()
                ArtikalKolicina = dbtStavke.Rows(cntx).Cells("KolicinaDataGridViewTextBoxColumn").Value()
                ArtikalCijena = dbtStavke.Rows(cntx).Cells("IznosDataGridViewTextBoxColumn").Value()


                Dim dsNewRow As DataRow = ds.Tables("RacunStavka").NewRow()
                dsNewRow.Item("BrRacun") = brojrc
                dsNewRow.Item("ArtikalNaziv") = ArtikalNaziv
                dsNewRow.Item("ArtikalJMJ") = ArtikalJMJ
                dsNewRow.Item("ArtikalKolicina") = ArtikalKolicina
                dsNewRow.Item("ArtikalCijena") = ArtikalCijena
                dsNewRow.Item("ArtikalIznos") = ArtikalIznos


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

                da.Update(ds, "RacunStavka")

                cntx = cntx + 1
            Loop

            cnn.Close()
            CTX = 1
        Catch ex As Exception
            MsgBox("Can not open connection ! " & ex.Message & vbCrLf & ex.StackTrace)
        End Try

I am quite sure that it's some silly error but can't quite figure out what. I am using mdb only in this case and it's used only to export some data from MDF database.

Error is:

"Object reference not set to an instance of an object"

It's breaking right here:

Dim dsNewRow As DataRow = ds.Tables("RacunStavka").NewRow()
dsNewRow.Item("BrRacun") = brojrc

Table and field exist and there is no typo. Checked at least five times ;)

I hope this is enough information. Thank you in advance.

Dani AI

Generated

A common cause for the behavior seen in this thread is attempting to create or update a DataRow in a DataTable that does not actually exist in the DataSet. later noted the problem was solved; for the benefit of others who hit the same pitfall, here are focused checks and fixes that apply to the DataSet / DataAdapter / CommandBuilder workflow.

  • Quick checks first: verify ds.Tables.Count > 0 and ds.Tables.Contains("YourTable") before calling NewRow. Inspect the adapter's SelectCommand and the DataSet after a Fill to confirm the schema is present. Make sure values read from the UI are not Nothing before assigning them into the DataRow.
  • If using a CommandBuilder, it needs a SELECT that returns column metadata so it can generate the INSERT/UPDATE/DELETE statements. Either call Fill with a SELECT that returns the table schema or create the DataTable (columns and key) programmatically and add it to the DataSet. See the docs for DataAdapter.Fill and OleDbCommandBuilder for details.

Two practical approaches:

  • Keep the DataAdapter/CommandBuilder flow: use a SELECT to fill the DataSet, create a new DataRow from that table, add it, then call Update so the adapter writes changes back.
    Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM YourTable", cnn)
    Dim cb As New OleDb.OleDbCommandBuilder(da)
    da.Fill(ds, "YourTable")
    Dim row As DataRow = ds.Tables("YourTable").NewRow()
    row("Col") = value
    ds.Tables("YourTable").Rows.Add(row)
    da.Update(ds, "YourTable")
  • Or use a parameterized command and ExecuteNonQuery for each row (often simpler and faster for inserts).
    Using cmd As New OleDbCommand("INSERT INTO YourTable (Col1,Col2) VALUES (?,?)", cnn)
    cmd.Parameters.AddWithValue("?", val1)
    cmd.Parameters.AddWithValue("?", val2)
    cmd.ExecuteNonQuery()
    End Using

Additional tips: avoid off‑by‑one loops over grid rows, wrap connections/commands in Using blocks to ensure disposal, and add basic null/type checks when reading UI values. Background reading: DataTable.NewRow, DataAdapter.Fill, OleDbCommandBuilder.

I figured a better way (better for me at least).
So this is solved.

Thanks !

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.