Reading Sequential access Files

propagandhi

Member
Joined
Feb 20, 2005
Messages
18
Programming Experience
Beginner
Alright everybody. I have to write a program which writes the store number, location, and store manager to a sequential access file. Im having no problem with this, but the program also has to be able to display a record based on the store number. When I run the program, i receive no errors but the corresponding record does nto display. Here is my code if you think you can help me.

Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click

Dim intIndexofFirstPound As Integer

Dim intIndexofSecondPound As Integer

Dim strRecord As String

Dim strNumber As String

Dim strNumber1 As String

Dim strState As String

Dim strManager As String

Dim sreStreamReader As IO.StreamReader

If IO.File.Exists("c:\Record.text") Then

sreStreamReader = IO.File.OpenText("c:\Record.txt")

strRecord = sreStreamReader.ReadLine()

'Find the index of the first pound sign

intIndexofFirstPound = strRecord.IndexOf("#")

'Pull off the store number starting at index 0 for a length

'of the index of the first pound sign index

strNumber = strRecord.Substring(0, intIndexofFirstPound)

'Determine the index of the second pound sign by starting at one 'position greater than the index of the first pound sign and

'and searching for the second pound sign from that point

intIndexofSecondPound = strRecord.IndexOf("#", intIndexofFirstPound + 1)

'Pull off the state by starting at one more than the index of the

'first pound sign for a length of the second pound sign index minus

'the first pound sign index minus 1

strState = strRecord.Substring(intIndexofFirstPound + 1, intIndexofSecondPound - intIndexofFirstPound - 1)

'Pull off the manager name by starting at one more than the index of

'the second pound sign

strManager = strRecord.Substring(intIndexofSecondPound + 1)

strNumber1 = txtStore.Text

If strNumber = strNumber1 Then

lb1.Items.Add(strNumber & strState & strManager)

sreStreamReader.Close()

End If

End If

End Sub

 

tlkerns

Member
Joined
Jun 3, 2004
Messages
19
Programming Experience
3-5
Try adding a breakpoint and then stepping through your code to see why the record isn't being displayed.

Tony
 

propagandhi

Member
Joined
Feb 20, 2005
Messages
18
Programming Experience
Beginner
ill try that, but does the code look like it should work?
 

tlkerns

Member
Joined
Jun 3, 2004
Messages
19
Programming Experience
3-5
Is this where you're trying to display the record?

lb1.Items.Add(strNumber & strState & strManager)

If so, then it is inside an If..Then statement that depends on the value of txtStore.Text.
Without knowing what txtStore.Text is, I can't tell you if the code will work. But if you step through it, you'll be able to see whether your item is being added to the listbox or not.

Tony
 

propagandhi

Member
Joined
Feb 20, 2005
Messages
18
Programming Experience
Beginner
alright, i cleaned up my code a little and had some success. Now my problem is that when the streamreader runs, it is only reading the first record. I think i have to use a loop or manually but in a end of record delimiter, but im not sure how. Anyways, here is the code for both the write and read.

Write:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

Dim strManager As String

Dim strStore As String

Dim strState As String

Dim swrStreamWriter As IO.StreamWriter

strState = cbState.Text

strStore = txtStore.Text

strManager = txtManager.Text

If Not IsNumeric(strStore) Then

MessageBox.Show("Non-Numeric Store Number", "Data Entry Error")

Else

If strState = "" Then

MessageBox.Show("Please Select a State", "Data Entry Error")

Else

If IsNumeric(strManager) Or strManager = "" Then

MessageBox.Show("Please Enter Manager Name", "Data Entry Error")

Else

Try

swrStreamWriter = IO.File.AppendText("Record.txt")

swrStreamWriter.WriteLine(strStore & "#" & strState & "#" & strManager)

swrStreamWriter.Close()

cbState.Text = ""

txtStore.Text = ""

txtManager.Text = ""

txtStore.Focus()

Catch ex As Exception

End Try

End If

End If

End If

Read:

Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click

Dim intIndexofFirstPound As Integer

Dim intIndexofSecondPound As Integer

Dim strRecord As String

Dim strNumber As String

Dim strNumber1 As String

Dim strState As String

Dim strManager As String

Dim sreStreamReader As IO.StreamReader

Dim strstore As String



If IO.File.Exists("Record.txt") Then

sreStreamReader = IO.File.OpenText("Record.txt")

strRecord = sreStreamReader.ReadLine()

intIndexofFirstPound = strRecord.IndexOf("#")

intIndexofSecondPound = strRecord.IndexOf("#", intIndexofFirstPound + 1)

strNumber = strRecord.Substring(0, intIndexofFirstPound)

strState = strRecord.Substring(intIndexofFirstPound + 1, intIndexofSecondPound - intIndexofFirstPound - 1)

strManager = strRecord.Substring(intIndexofSecondPound + 1)

strstore = txtStore.Text

If strNumber = strstore Then

lb1.Items.Add(strNumber & " " & strState & strManager)

sreStreamReader.Close()

End If

End If

End Sub

 
Top Bottom