Im using this code to import .dat file to SQLite Table
My connection
Public strConn As String = "Data Source =DTS.db;Version=3;"
My Procedure
Private Sub btnImportDB_Click(sender As Object, e As EventArgs) Handles btnImportDB.Click
If txtDatFile.Text = String.Empty Then Exit Sub
Dim fname As String = txtDatFile.Text
Dim colsexpected As Integer = 6
Dim Filereader As New StreamReader(fname, Encoding.Default)
Dim sLine As String = String.Empty
Using MyConn As New SQLiteConnection(strConn)
Do
sLine = Filereader.ReadLine
If sLine Is Nothing Then Exit Do
Dim vArray() As String = sLine.Split(CType(vbTab, Char()))
Dim InsertData As String = "INSERT INTO tblTempData (fldMacId,fldDateTime,fldRem1,fldRem2,fldRem3,fldRem4) Values (@MacId,@xDateTime,@Rem1,@Rem2,@Rem3,@Rem4)"
Dim cmd As New SQLiteCommand(InsertData, MyConn)
cmd.Parameters.AddWithValue("@MacId", vArray(0))
cmd.Parameters.AddWithValue("@xDateTime", vArray(1))
cmd.Parameters.AddWithValue("@Rem1", vArray(2))
cmd.Parameters.AddWithValue("@Rem2", vArray(3))
cmd.Parameters.AddWithValue("@Rem3", vArray(4))
cmd.Parameters.AddWithValue("@Rem4", vArray(5))
Try
With MyConn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
MyConn.Open()
cmd.ExecuteNonQuery()
Cursor.Current = Cursors.WaitCursor
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Loop
End Using
MessageBox.Show("Records successfully imported.", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtDatFile.Clear()
End Sub
This is the error
{"Operation is not valid due to the current state of the object."}
What I have tried:
I cant find which part cause the error..