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.

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.