Help with Advancing to Next Record, read line continued iteration problem

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 1
Reputation: noseforwine is an unknown quantity at this point 
Solved Threads: 0
noseforwine noseforwine is offline Offline
Newbie Poster

Help with Advancing to Next Record, read line continued iteration problem

 
0
  #1
Dec 6th, 2008
I have designed a form to open a .txt file, read the values and display them into the forms label controls. My problem is that I cant figure out how to advance to read the next record when I click on the next record btn. I am able to read the first 9 lines that are written to the file, but nothing after that. I need to be able to read lines 10 thru 19 and display them in the forms labels when the btnNext is clicked, Then display the next set of values lines 20 thru 29 when btnNext is clicked again. I think I need to figure out a way to increment the record count and reader.

Below is a sample of the .txt file I am opening as well as the forms code.

Any help or suggestions would be greatly appreciated. Thanks

Example of file lay out:

Record Number
First Name
Middle Name
Last Name
Employee Number
Dept
Phone
Extension
Email
__________

File Values
_________
462
Mike
B
Davis
1122
Accounting
322-0009
112
mike@aol.com
612
Sam
L
Jackson
1048
Administration
966-7589
2
sam@aol.com
398
Lin
P
Daniels
1014
Marketing
966-2217
5
Lin@aol.com
___________

Code Example:
  1. Option Strict On
  2. Imports System.IO
  3.  
  4. Public Class frmMain
  5. Inherits System.Windows.Forms.Form
  6.  
  7.  
  8. Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  9.  
  10. ' Let the user select a file to open. Pass the selected file to the ReadFile procedure.
  11. With ofdOpenFile
  12. .Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
  13. .Title = "Select a File to Open"
  14. If .ShowDialog = Windows.Forms.DialogResult.OK Then
  15. If .FileName <> String.Empty Then
  16. ReadFile(.FileName)
  17. Else
  18. MessageBox.Show("No file selected.", "Error")
  19. End If
  20. End If
  21. End With
  22.  
  23.  
  24. End Sub
  25.  
  26. Sub ReadFile(ByVal strFileName As String)
  27.  
  28. ' Read the contents of the specified file into the UnitsSold array.
  29.  
  30. Dim inputFile As StreamReader
  31. Dim intCount As Integer = 1
  32.  
  33. ' Declare variables.
  34. Dim intN As Integer
  35. Dim strFirst As String
  36. Dim strMiddle As String
  37. Dim strLast As String
  38. Dim intEmpNum As Integer
  39. Dim strDept As String
  40. Dim strPhone As String
  41. Dim intExt As Integer
  42. Dim strEmail As String
  43.  
  44. ' Open the file.
  45. inputFile = File.OpenText(strFileName)
  46.  
  47. ' Read the data.
  48. For intCount = 1 To 1
  49. ' Read the record from the file.
  50. intN = CInt(inputFile.ReadLine())
  51. strFirst = inputFile.ReadLine()
  52. strMiddle = inputFile.ReadLine()
  53. strLast = inputFile.ReadLine()
  54. intEmpNum = CInt(inputFile.ReadLine())
  55. strDept = inputFile.ReadLine()
  56. strPhone = inputFile.ReadLine()
  57. intExt = CInt(inputFile.ReadLine())
  58. strEmail = inputFile.ReadLine()
  59.  
  60. ' Display the input data on the record form.
  61.  
  62. lblN.Text = intN.ToString()
  63. lblFirst.Text = strFirst.ToString()
  64. lblMiddle.Text = strMiddle.ToString()
  65. lblLast.Text = strLast.ToString()
  66. lblEmpNum.Text = intEmpNum.ToString()
  67. lblDept.Text = strDept.ToString()
  68. lblPhone.Text = strPhone.ToString()
  69. lblExt.Text = intExt.ToString()
  70. lblEmail.Text = strEmail.ToString()
  71. Next intCount
  72.  
  73. ' Close the file.
  74. inputFile.Close()
  75.  
  76. End Sub
  77.  
  78. Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
  79.  
  80. ' Here is where I need some help!
  81.  
  82. End Sub
  83. End Class
Last edited by noseforwine; Dec 6th, 2008 at 9:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

Re: Help with Advancing to Next Record, read line continued iteration problem

 
0
  #2
Dec 7th, 2008
The reason is because in your ReadFile you are opening the file, reading a set of data and then closing the file. The next time you use it, it is reading the same record.
Move the Input.Close() to a button to close it and also in FormClosing. However, this is only going to let you go one way in the file, top to bottom, and you will not be able to make changes to the file.
Why not use a database or CSV?
Last edited by waynespangler; Dec 7th, 2008 at 6:33 am.
Wayne

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC