VB Code:
  1. The following program, reads a text file, and adds it to the database; by creating a new table and records for it.
  2.  
  3. However, I have a problem at the end, where I get the error when the compiler executes "ct.NewRow.AcceptChanges()"
  4.  
  5. Any help would be appreciated, thanks.
  6.  
  7.  
  8. Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
  9.         Dim AllText As String = "", LineOfText As String = ""
  10.         OpenFormDialog.Filter = "Text files (*.TXT)|*.TXT" 'Display only Text Files
  11.         OpenFormDialog.ShowDialog() 'Display Dialog Boz
  12.         If OpenFormDialog.FileName <> "" Then
  13.             Try 'open file and catch any errors
  14.                 FileOpen(1, OpenFormDialog.FileName, OpenMode.Input)
  15.                 Dim conn As New RECORDSDataSet
  16.                 Dim tablename = 1
  17.                 Dim ct As DataTable = conn.Tables.Add(tablename)
  18.  
  19.                 ct.Columns.Add("CustomerNumber", GetType(Int16))
  20.                 ct.Columns.Add("CustomerName", GetType(String))
  21.                 ct.Columns.Add("MeterNumber", GetType(String))
  22.                 ct.Columns.Add("PreviousMonth", GetType(Double))
  23.                 ct.Columns.Add("Owed", GetType(Double))
  24.                 ct.Columns.Add("Paid", GetType(Double))
  25.                 ct.Columns.Add("CurrentMonth", GetType(Double))
  26.  
  27.                 Do Until EOF(1) 'Read lines from file, stop when it reaches the end
  28.                     LineOfText = LineInput(1)
  29.                     'add each line to the AllText variable
  30.                     Call CreateDatabase(LineOfText, conn, ct)
  31.                 Loop
  32.             Catch
  33.                 MsgBox("Error Opening File") 'Display message box if file cannot be opened
  34.             Finally
  35.                 FileClose(1) 'close file
  36.             End Try
  37.         End If
  38.     End Sub
  39.  
  40.     Sub CreateDatabase(ByVal LineOfText As String, ByRef conn As RECORDSDataSet, ByRef ct As DataTable)
  41.         'Variables that will be required for input into the database
  42.         Dim arr() As String = Split(LineOfText, " ")
  43.         Dim CustomerNumber, LastMonth, CurrentHours As Integer
  44.         Dim Owed, Paid As Double
  45.         Dim CustomerName, MeterNumber As String
  46.  
  47.         CustomerNumber = arr(0)
  48.         CustomerName = arr(1) & arr(2) 'Combine first and last name
  49.         MeterNumber = arr(3)
  50.         LastMonth = arr(4)
  51.         Owed = arr(5)
  52.         Paid = arr(6)
  53.         CurrentHours = arr(7)
  54.  
  55.         'Adds new row
  56.         ct.NewRow.BeginEdit()
  57.         ct.NewRow.Item(0) = CustomerNumber
  58.         ct.NewRow.Item(1) = CustomerName
  59.         ct.NewRow.Item(2) = MeterNumber
  60.         ct.NewRow.Item(3) = LastMonth
  61.         ct.NewRow.Item(4) = Owed
  62.         ct.NewRow.Item(5) = Paid
  63.         ct.NewRow.Item(6) = CurrentHours
  64.         'PROBLEM HERE, in STEP INTO MODE, THIS IS WHERE THE ERROR IS CAUGHT
  65.         ct.NewRow.AcceptChanges()
  66.         ct.NewRow.EndEdit()
  67.     End Sub
  68. End Class