Results 1 to 9 of 9

Thread: Create New Table Row Error

  1. #1

    Thread Starter
    Junior Member thekoolgeek's Avatar
    Join Date
    Jan 2006
    Location
    Omaha, NE
    Posts
    20

    Create New Table Row Error

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Create New Table Row Error

    Firstly, I believe that you've already been told not to call AcceptChanges in a previous thread.

    Secondly, NewRow is NOT a property. It is a function that creates a new row and returns it. That means that every time you call NewRow it creates a new row. You're calling it 10 times so you're creating 10 rows. Also, you never keep a reference to any of those rows and add it to the table. Create a single new row, assign it to a variable, set its fields, do NOT call AcceptChanges, then finally add the row to the table that created it.

    Finally, that FileOpen/LineInput/FileClose is an old method of I/O that should never be used in new code. You should use members of the System.IO namespace, e.g. StreamReader, for .NET I/O operations.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member thekoolgeek's Avatar
    Join Date
    Jan 2006
    Location
    Omaha, NE
    Posts
    20

    Re: Create New Table Row Error

    Quote Originally Posted by jmcilhinney
    Firstly, I believe that you've already been told not to call AcceptChanges in a previous thread.

    Secondly, NewRow is NOT a property. It is a function that creates a new row and returns it. That means that every time you call NewRow it creates a new row. You're calling it 10 times so you're creating 10 rows. Also, you never keep a reference to any of those rows and add it to the table. Create a single new row, assign it to a variable, set its fields, do NOT call AcceptChanges, then finally add the row to the table that created it.

    Finally, that FileOpen/LineInput/FileClose is an old method of I/O that should never be used in new code. You should use members of the System.IO namespace, e.g. StreamReader, for .NET I/O operations.

    First of all, I've never been told that.
    Secondly, ok.
    And finally, thats how I learned it from my Visual Basic 2005 Programmer's Reference book. It works and you know what they say "if it ain't broken, don't fix it."

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Create New Table Row Error

    Firstly, sorry, my mistake. Must have been someone else asking a very similar question.

    Secondly, cool.

    Thirdly, that's extremely dodgy. Anyone writing a VB 2005 reference book should be shot for including code like that. Microsoft explicitly state that that form of I/O is included for legacy support and may degrade performance. It's only there to support upgraded VB6 apps and should never be used in new code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member thekoolgeek's Avatar
    Join Date
    Jan 2006
    Location
    Omaha, NE
    Posts
    20

    Re: Create New Table Row Error

    Quote Originally Posted by jmcilhinney
    Firstly, I believe that you've already been told not to call AcceptChanges in a previous thread.

    Secondly, NewRow is NOT a property. It is a function that creates a new row and returns it. That means that every time you call NewRow it creates a new row. You're calling it 10 times so you're creating 10 rows. Also, you never keep a reference to any of those rows and add it to the table. Create a single new row, assign it to a variable, set its fields, do NOT call AcceptChanges, then finally add the row to the table that created it.

    Finally, that FileOpen/LineInput/FileClose is an old method of I/O that should never be used in new code. You should use members of the System.IO namespace, e.g. StreamReader, for .NET I/O operations.

    Now about step two here, can I get some code for that?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Create New Table Row Error

    You already know how. How would you normally call a function and assign the result to a variable? You know how because you're already doing it, e.g.
    VB Code:
    1. Dim ct As DataTable = conn.Tables.Add(tablename)
    You also know how set a field of a DataRow because you're already doing that too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member thekoolgeek's Avatar
    Join Date
    Jan 2006
    Location
    Omaha, NE
    Posts
    20

    Re: Create New Table Row Error

    This is what I have:

    VB Code:
    1. Sub CreateDatabase(ByVal LineOfText As String, ByRef conn As RECORDSDataSet, ByVal ct As DataTable, ByVal FileName As String)
    2.         'Variables that will be required for input into the database
    3.         Dim arr() As String = Split(LineOfText, " ")
    4.         Dim CustomerNumber, LastMonth, CurrentHours As Integer
    5.         Dim Owed, Paid As Double
    6.         Dim CustomerName, MeterNumber As String
    7.         Dim NR As DataRow
    8.  
    9.         CustomerNumber = arr(0)
    10.         CustomerName = arr(1) & arr(2) 'Combine first and last name
    11.         MeterNumber = arr(3)
    12.         LastMonth = arr(4)
    13.         Owed = arr(5)
    14.         Paid = arr(6)
    15.         CurrentHours = arr(7)
    16.  
    17.         NR = conn.Tables(FileName).NewRow
    18.  
    19.         NR.BeginEdit()
    20.         NR.Item(0) = CustomerNumber
    21.         NR.Item(1) = CustomerName
    22.         NR.Item(2) = MeterNumber
    23.         NR.Item(3) = LastMonth
    24.         NR.Item(4) = Owed
    25.         NR.Item(5) = Paid
    26.         NR.Item(6) = CurrentHours
    27.         NR.EndEdit()
    28.  
    29.         conn.Tables(FileName).Rows.Add(NR)
    30.  
    31.     End Sub

    Its giving me an error, so how would i fix it?
    If there is no such thing as stupid questions, how do stupid people ask questions? Do they get smart right before they ask the question?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Create New Table Row Error

    Quote Originally Posted by thekoolgeek
    Its giving me an error, so how would i fix it?
    How would we know if we don't know what the error is?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Junior Member thekoolgeek's Avatar
    Join Date
    Jan 2006
    Location
    Omaha, NE
    Posts
    20

    Re: Create New Table Row Error

    This is everything that I have. It is not returning any errors, but the Table is not getting created.

    VB Code:
    1. Imports System.Data
    2. Imports System.Data.OleDb
    3.  
    4.  
    5. Public Class frmMain
    6.  
    7.     'Images that will be used for buttons
    8.     Dim btnBG As System.Drawing.Image = Image.FromFile("C:\Documents and Settings\Allan Hernandez\My Documents\Visual Studio 2005\Projects\E-Z Watt Electric Co\E-Z Watt Electric Co\btnbg.jpeg")
    9.     Dim btnBG2 As System.Drawing.Image = Image.FromFile("C:\Documents and Settings\Allan Hernandez\My Documents\Visual Studio 2005\Projects\E-Z Watt Electric Co\E-Z Watt Electric Co\btnbg2.jpeg")
    10.  
    11.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    12.         'Change Button Backgrounds
    13.         btnUpload.BackgroundImage = btnBG2
    14.         btnReports.BackgroundImage = btnBG2
    15.         btnExit.BackgroundImage = btnBG2
    16.     End Sub
    17.  
    18.     Private Sub btnUpload_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.MouseHover
    19.         'Change background when the mouse moves over the button.
    20.         btnUpload.BackgroundImage = btnBG
    21.         slMain.Text = "Upload a File for processing."
    22.     End Sub
    23.  
    24.     Private Sub btnReports_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReports.MouseHover
    25.         'Change background when the mouse moves over the button.
    26.         btnReports.BackgroundImage = btnBG
    27.         slMain.Text = "Print/View Month's Reports"
    28.     End Sub
    29.  
    30.     Private Sub btnExit_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.MouseHover
    31.         'Change background when the mouse moves over the button.        
    32.         btnExit.BackgroundImage = btnBG
    33.         slMain.Text = "Exit the application"
    34.     End Sub
    35.  
    36.     Private Sub btnUpload_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.MouseLeave
    37.         'Change background when the mouse leaves the button.
    38.         btnUpload.BackgroundImage = btnBG2
    39.         slMain.Text = ""
    40.     End Sub
    41.  
    42.     Private Sub btnReports_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReports.MouseLeave
    43.         'Change background when the mouse leaves the button.
    44.         btnReports.BackgroundImage = btnBG2
    45.         slMain.Text = ""
    46.     End Sub
    47.  
    48.     Private Sub btnExit_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.MouseLeave
    49.         'Change background when the mouse leaves the button.
    50.         btnExit.BackgroundImage = btnBG2
    51.         slMain.Text = ""
    52.     End Sub
    53.  
    54.     Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    55.         End
    56.     End Sub
    57.  
    58.     Private Sub tmrTime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTime.Tick
    59.         'Aqcuires current date and time for label
    60.         Dim CurrentDate As DateTime = DateTime.Now
    61.  
    62.         'Updates label with current Time, and Company Name
    63.         lblTime.Text = "E-Z Watt Electric Co-Op: " & CurrentDate
    64.     End Sub
    65.  
    66.     Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
    67.         Dim FileName As String = "", LineOfText As String = ""
    68.         OpenFormDialog.Filter = "Text files (*.TXT)|*.TXT" 'Display only Text Files
    69.         OpenFormDialog.ShowDialog() 'Display Dialog Boz
    70.         If OpenFormDialog.FileName <> "" Then
    71.             FileName = OpenFormDialog.FileName
    72.             Try 'open file and catch any errors
    73.                 FileOpen(1, OpenFormDialog.FileName, OpenMode.Input)
    74.                 Dim conn As New RECORDSDataSet
    75.                 Dim tablename = 1
    76.                 Dim ct As DataTable = conn.Tables.Add(FileName)
    77.  
    78.                 ct.Columns.Add("CustomerNumber", GetType(Integer))
    79.                 ct.Columns.Add("CustomerName", GetType(String))
    80.                 ct.Columns.Add("MeterNumber", GetType(String))
    81.                 ct.Columns.Add("PreviousMonth", GetType(Double))
    82.                 ct.Columns.Add("Owed", GetType(Double))
    83.                 ct.Columns.Add("Paid", GetType(Double))
    84.                 ct.Columns.Add("CurrentMonth", GetType(Double))
    85.  
    86.                 Do Until EOF(1) 'Read lines from file, stop when it reaches the end
    87.                     LineOfText = LineInput(1)
    88.                     'add each line to the AllText variable
    89.                     Call CreateDatabase(LineOfText, conn, ct, FileName)
    90.                 Loop
    91.             Catch
    92.                 MsgBox("Error Opening File") 'Display message box if file cannot be opened
    93.             Finally
    94.                 FileClose(1) 'close file
    95.             End Try
    96.         End If
    97.     End Sub
    98.  
    99.     Sub CreateDatabase(ByVal LineOfText As String, ByRef conn As RECORDSDataSet, ByVal ct As DataTable, ByVal FileName As String)
    100.         'Variables that will be required for input into the database
    101.         Dim arr() As String = Split(LineOfText, " ")
    102.         Dim CustomerNumber, LastMonth, CurrentHours As Integer
    103.         Dim Owed, Paid As Double
    104.         Dim CustomerName, MeterNumber As String
    105.         Dim NR As DataRow
    106.  
    107.         CustomerNumber = arr(0)
    108.         CustomerName = arr(1) & arr(2) 'Combine first and last name
    109.         MeterNumber = arr(3)
    110.         LastMonth = arr(4)
    111.         Owed = arr(5)
    112.         Paid = arr(6)
    113.         CurrentHours = arr(7)
    114.  
    115.         NR = conn.Tables(FileName).NewRow
    116.  
    117.         NR.BeginEdit()
    118.         NR.Item(0) = CustomerNumber
    119.         NR.Item(1) = CustomerName
    120.         NR.Item(2) = MeterNumber
    121.         NR.Item(3) = LastMonth
    122.         NR.Item(4) = Owed
    123.         NR.Item(5) = Paid
    124.         NR.Item(6) = CurrentHours
    125.         NR.EndEdit()
    126.  
    127.         conn.Tables(FileName).Rows.Add(NR)
    128.     End Sub
    129. End Class

    Any help would be appreciated.
    If there is no such thing as stupid questions, how do stupid people ask questions? Do they get smart right before they ask the question?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width