I'm trying to read a DBF file into a DataSet and then write a selected number of columns to a MDB.
I've been successful reading the DBF into the Dataset with this code;
Public Sub sReadDbf(ByVal dbffile As String)
mydbfConn = New System.Data.OleDb.OleDbConnection
mydbfConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
sAlohaPath & "\" & dbfFileFolder & "\ ;Extended Properties=dBASE IV;User ID=Admin;Password=;"
mydbfConn.Open()
Dim ds As New DataSet
Try
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM GndItem" & dbfFileName, mydbfConn)
da.Fill(ds, "Data")
Me.dgv1.DataSource = ds.Tables("Data")
mydbfConn.Close()
Catch ex As Exception
Using writer As StreamWriter = New StreamWriter(sAlohaPath & "\" & dbfFileFolder & "\" & sLog, True)
writer.WriteLine(Now & ": " & dbffile & " >---ERROR---< " & ex.Message)
End Using
End Try
End Sub
Now I'm trying write the data to the MDB and I can't get past the initial part of this code. It crashes at the part where I'm just testing to see the number of rows in my DataSet table(0); MsgBox(ds.Tables("Data").Rows.Count) with error: NullReferenceException was unhandled & Object reference not set to an instance of an object.
Public Sub sWriteAccess()
myAccConn = New OleDb.OleDbConnection
Dim cmd As New OleDb.OleDbCommand
myAccConn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & Application.StartupPath & "\Gnditem.mdb"
If Not myAccConn.State = ConnectionState.Open Then
'open connection
myAccConn.Open()
End If
MsgBox(ds.Tables("Data").Rows.Count) '''CRASH HERE!!!!
Dim dtRows As Integer = Me.ds.Tables(0).Rows.Count
Try
For iX = 0 To dtRows - 1
cmd.CommandText = "INSERT INTO GrindItems ( EMPLOYEE, CHECK, ITEM, PARENT, CATEGORY ) " & _
" VALUES(" & _
ds.Tables(0).Rows(iX).Item(1) & "," & _
ds.Tables(0).Rows(iX).Item(2) & "," & _
ds.Tables(0).Rows(iX).Item(3) & "," & _
ds.Tables(0).Rows(iX).Item(4) & "," & _
ds.Tables(0).Rows(iX).Item(5) & ")"
cmd.ExecuteNonQuery()
Next
Catch ex As Exception
End Try
myAccConn.Close()
End Sub
I can't figure out why the DataSet seems to be not available in the 2nd Sub, or where/how to set the instance.
I've got no idea whether I've got the rest of this INSERT query will work.
Appreciate some help & guidance here.