|
-
Feb 6th, 2007, 09:38 PM
#1
Thread Starter
Junior Member
Create New Table Row Error
VB Code:
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
-
Feb 6th, 2007, 10:04 PM
#2
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.
-
Feb 6th, 2007, 10:33 PM
#3
Thread Starter
Junior Member
Re: Create New Table Row Error
 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."
-
Feb 6th, 2007, 10:41 PM
#4
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.
-
Feb 7th, 2007, 05:05 PM
#5
Thread Starter
Junior Member
Re: Create New Table Row Error
 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?
-
Feb 7th, 2007, 05:39 PM
#6
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:
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.
-
Feb 7th, 2007, 06:52 PM
#7
Thread Starter
Junior Member
Re: Create New Table Row Error
This is what I have:
VB Code:
Sub CreateDatabase(ByVal LineOfText As String, ByRef conn As RECORDSDataSet, ByVal ct As DataTable, ByVal FileName As String)
'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
Dim NR As DataRow
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)
NR = conn.Tables(FileName).NewRow
NR.BeginEdit()
NR.Item(0) = CustomerNumber
NR.Item(1) = CustomerName
NR.Item(2) = MeterNumber
NR.Item(3) = LastMonth
NR.Item(4) = Owed
NR.Item(5) = Paid
NR.Item(6) = CurrentHours
NR.EndEdit()
conn.Tables(FileName).Rows.Add(NR)
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?
-
Feb 7th, 2007, 07:05 PM
#8
Re: Create New Table Row Error
 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?
-
Feb 7th, 2007, 07:28 PM
#9
Thread Starter
Junior Member
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:
Imports System.Data
Imports System.Data.OleDb
Public Class frmMain
'Images that will be used for buttons
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")
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")
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Change Button Backgrounds
btnUpload.BackgroundImage = btnBG2
btnReports.BackgroundImage = btnBG2
btnExit.BackgroundImage = btnBG2
End Sub
Private Sub btnUpload_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.MouseHover
'Change background when the mouse moves over the button.
btnUpload.BackgroundImage = btnBG
slMain.Text = "Upload a File for processing."
End Sub
Private Sub btnReports_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReports.MouseHover
'Change background when the mouse moves over the button.
btnReports.BackgroundImage = btnBG
slMain.Text = "Print/View Month's Reports"
End Sub
Private Sub btnExit_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.MouseHover
'Change background when the mouse moves over the button.
btnExit.BackgroundImage = btnBG
slMain.Text = "Exit the application"
End Sub
Private Sub btnUpload_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.MouseLeave
'Change background when the mouse leaves the button.
btnUpload.BackgroundImage = btnBG2
slMain.Text = ""
End Sub
Private Sub btnReports_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReports.MouseLeave
'Change background when the mouse leaves the button.
btnReports.BackgroundImage = btnBG2
slMain.Text = ""
End Sub
Private Sub btnExit_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.MouseLeave
'Change background when the mouse leaves the button.
btnExit.BackgroundImage = btnBG2
slMain.Text = ""
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
End
End Sub
Private Sub tmrTime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTime.Tick
'Aqcuires current date and time for label
Dim CurrentDate As DateTime = DateTime.Now
'Updates label with current Time, and Company Name
lblTime.Text = "E-Z Watt Electric Co-Op: " & CurrentDate
End Sub
Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
Dim FileName As String = "", LineOfText As String = ""
OpenFormDialog.Filter = "Text files (*.TXT)|*.TXT" 'Display only Text Files
OpenFormDialog.ShowDialog() 'Display Dialog Boz
If OpenFormDialog.FileName <> "" Then
FileName = OpenFormDialog.FileName
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(FileName)
ct.Columns.Add("CustomerNumber", GetType(Integer))
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, FileName)
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, ByVal ct As DataTable, ByVal FileName As String)
'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
Dim NR As DataRow
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)
NR = conn.Tables(FileName).NewRow
NR.BeginEdit()
NR.Item(0) = CustomerNumber
NR.Item(1) = CustomerName
NR.Item(2) = MeterNumber
NR.Item(3) = LastMonth
NR.Item(4) = Owed
NR.Item(5) = Paid
NR.Item(6) = CurrentHours
NR.EndEdit()
conn.Tables(FileName).Rows.Add(NR)
End Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|