The following program, reads a text file, and adds it to the database; by creating a new table and records for it.
However, I have a problem at the end, where I get the error when the compiler executes "ct.NewRow.AcceptChanges()"
Any help would be appreciated, thanks.
Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Dim AllText As String = "", LineOfText As String = ""
OpenFormDialog.Filter = "Text files (*.TXT)|*.TXT" 'Display only Text Files
OpenFormDialog.ShowDialog() 'Display Dialog Boz
If OpenFormDialog.FileName <> "" Then
Try 'open file and catch any errors
FileOpen(1, OpenFormDialog.FileName, OpenMode.Input)
Dim conn As New RECORDSDataSet
Dim tablename = 1
Dim ct As DataTable = conn.Tables.Add(tablename)
ct.Columns.Add("CustomerNumber", GetType(Int16))
ct.Columns.Add("CustomerName", GetType(String))
ct.Columns.Add("MeterNumber", GetType(String))
ct.Columns.Add("PreviousMonth", GetType(Double))
ct.Columns.Add("Owed", GetType(Double))
ct.Columns.Add("Paid", GetType(Double))
ct.Columns.Add("CurrentMonth", GetType(Double))
Do Until EOF(1) 'Read lines from file, stop when it reaches the end
LineOfText = LineInput(1)
'add each line to the AllText variable
Call CreateDatabase(LineOfText, conn, ct)
Loop
Catch
MsgBox("Error Opening File") 'Display message box if file cannot be opened
Finally
FileClose(1) 'close file
End Try
End If
End Sub
Sub CreateDatabase(ByVal LineOfText As String, ByRef conn As RECORDSDataSet, ByRef ct As DataTable)
'Variables that will be required for input into the database
Dim arr() As String = Split(LineOfText, " ")
Dim CustomerNumber, LastMonth, CurrentHours As Integer
Dim Owed, Paid As Double
Dim CustomerName, MeterNumber As String
CustomerNumber = arr(0)
CustomerName = arr(1) & arr(2) 'Combine first and last name
MeterNumber = arr(3)
LastMonth = arr(4)
Owed = arr(5)
Paid = arr(6)
CurrentHours = arr(7)
'Adds new row
ct.NewRow.BeginEdit()
ct.NewRow.Item(0) = CustomerNumber
ct.NewRow.Item(1) = CustomerName
ct.NewRow.Item(2) = MeterNumber
ct.NewRow.Item(3) = LastMonth
ct.NewRow.Item(4) = Owed
ct.NewRow.Item(5) = Paid
ct.NewRow.Item(6) = CurrentHours
'PROBLEM HERE, in STEP INTO MODE, THIS IS WHERE THE ERROR IS CAUGHT
ct.NewRow.AcceptChanges()
ct.NewRow.EndEdit()
End Sub
End Class