Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,460 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,758 people online right now. Registration is fast and FREE... Join Now!




VB.Net 2005 Read and Write a Sequential File

4 Pages V  1 2 3 > »   
Reply to this topicStart new topic

VB.Net 2005 Read and Write a Sequential File, Sequential file contains a rolodex (richtext file)

bravo659
7 Aug, 2008 - 10:59 PM
Post #1

New D.I.C Head
*

Joined: 18 May, 2008
Posts: 32

I have to read and write a sequential file that contains a rolodex (richtext file). I have 6 textboxes for 6 fields lastName, firstName, Address, ContactDate, City, State, and a maskedTextbox for ZipCode, and richtextbox for notes. I am having a problem writing code for the textboxes that suppose to navigate from one record at a time. Here is my code:
I appreciate for your help. This is written in Visual Basic 2005 from the book Programming with Microsoft VB 2005 Object Oriented Approach byy Michael Ekedahl, 2007.
CODE

Option Explicit On
Option Strict On

Imports System.Convert
Imports System.IO

' The ContactReader class is used to read the
' sequential file containing the contacts.
Public Class RolodexList

    Private CurrentReader As StreamReader

    ' Create an instance of the StreamReader when the
    ' class instance is created.
    Public Sub New(ByVal argFile As String)
        CurrentReader = New StreamReader(argFile)
    End Sub

    ' Read the contact file.
    Public Function ReadContact() As Rolodex
        Dim CurrentContact As Rolodex
        Dim Fields() As String
        Dim CurrentRecord As String

        ' Define the delimiter character.
        Dim DelimiterChars() As Char = {ToChar(",")}

        ' Read a line from the file.
        CurrentRecord = CurrentReader.ReadLine()

        ' Split the line into the respective fields.
        Fields = CurrentRecord.Split(DelimiterChars)

        ' Populate the contact record.
        With CurrentContact
            .lastName = Fields(0)
IndexOutOfRangeException--->    .firstName = Fields(1) <-- My error
            .Address = Fields(2)
            .ContactDate = ToDateTime(Fields(3))
            .City = Fields(4)
            .State = Fields(5)
            .ZipCode = Fields(6)

        End With

        ' Return the contact record.
        Return CurrentContact
    End Function

    ' Determine whether end of file is true by
    ' trying to examine the next character. End of file
    ' is true if there are no more characters.
    Function EndOfFile() As Boolean
        Dim NextCharacter As Integer
        NextCharacter = CurrentReader.Peek()
        If NextCharacter = -1 Then
            Return True
        End If
    End Function

    ' Close the file referenced by CurrentReader.
    Public Sub Close()
        CurrentReader.Close()
    End Sub
End Class

' Declare the structure named Contact to store
' the contact information.
Public Structure Rolodex
    Public lastName As String
    Public firstName As String
    Public Address As String
    Public ContactDate As Date
    Public City As String
    Public State As String
    Public ZipCode As String

End Structure

Option Explicit On
Option Strict On
Imports System.IO
Imports System.Convert

Public Class Form1

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

        If My.Computer.Keyboard.NumLock = True Then
            tsslNumLock.Text = "Num"
        Else
            tsslNumLock.Text = ""

        End If

        If My.Computer.Keyboard.CapsLock = True Then
            tsslCapsLock.Text = "Caps"
        Else
            tsslCapsLock.Text = ""

        End If

        If My.Computer.Keyboard.ScrollLock = True Then
            tsslScrollLock.Text = "Scroll"
        Else
            tsslScrollLock.Text = ""

        End If
    End Sub

    
    Private Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click

        Dim Result As DialogResult

        Result = colorDialog.ShowDialog

        If Result = Windows.Forms.DialogResult.OK Then
            rtbMain.SelectionColor = colorDialog.Color

        End If
    End Sub

    Private Sub FontToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click

        Dim result As DialogResult

        result = fontDialog.ShowDialog

        If result = Windows.Forms.DialogResult.OK Then
            rtbMain.SelectionFont = fontDialog.Font

        End If

    End Sub
    Private Sub OpenFile()

        Dim Result As DialogResult

        openDialog.Filter = "Rich Text(*.rtf)|*.rtf" & _
        "Text Files(*.txt)|(*.txt)|*.txt|All Files(*.*)|*.*"
        openDialog.FilterIndex = 2

        Result = openDialog.ShowDialog

        If Result = Windows.Forms.DialogResult.OK Then
            rtbMain.LoadFile(openDialog.FileName, _
            RichTextBoxStreamType.PlainText)

        End If

        tsslFileName.Text = openDialog.FileName


    End Sub
  

    Private Sub SaveFile()

        Dim result As DialogResult

        result = saveDialog.ShowDialog

        If result = Windows.Forms.DialogResult.OK Then
            rtbMain.SaveFile(saveDialog.FileName, _
            RichTextBoxStreamType.RichText)

        End If
    End Sub

    Private Sub NavigateFirst()

        rtbMain.SelectionStart = 0
        rtbMain.SelectionLength = 0

    End Sub

    Private Sub NavigateLast()

        rtbMain.SelectionStart = rtbMain.Text.Length - 1

    End Sub

    Private Sub mnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOpen.Click

        Call OpenFile()


    End Sub

    Private Sub mnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSave.Click

        Call SaveFile()

    End Sub

    Private Sub tsFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsFirst.Click

        Call NavigateFirst()

    End Sub

    Private Sub tsLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsLast.Click

        Call NavigateLast()

    End Sub
    Private CurrentRolodexList() As Rolodex
    Private CurrentRecord As Integer = 0
    Private NextRecord As Integer = 0

    Private Sub tsOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsOpen.Click
        Dim CurrentReader As RolodexList
        Dim CurrentContact As Rolodex
        Dim Result As DialogResult
        openDialog.Filter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*"
        Result = openDialog.ShowDialog
        If Result = Windows.Forms.DialogResult.OK Then

            ' Create the instance of the ContactReader class thereby creating
            ' the underlying StreamReader
            CurrentReader = New RolodexList(openDialog.FileName)

            ' Read and proces until the end of file has been reached.
            Do Until CurrentReader.EndOfFile
                CurrentContact = CurrentReader.ReadContact
                ReDim Preserve CurrentRolodexList(NextRecord)
                CurrentRolodexList(NextRecord) = CurrentContact
                NextRecord += 1
            Loop
            CurrentReader.Close()
        End If


        Call DisplayCurrentRecord(CurrentRecord)


    End Sub

    Private Sub DisplayCurrentRecord(ByVal index As Integer)
        txtfirstName.Text = CurrentRolodexList(index).firstName
        txtlastName.Text = CurrentRolodexList(index).lastName
        txtAddress.Text = CurrentRolodexList(index).Address
        txtContactDate.Text = CurrentRolodexList(index).ContactDate.ToShortDateString
        txtCity.Text = CurrentRolodexList(index).City
        txtState.Text = CurrentRolodexList(index).State
        mtbZipCode.Text = CurrentRolodexList(index).ZipCode

    End Sub
End Class


This post has been edited by bravo659: 7 Aug, 2008 - 11:28 PM
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: VB.Net 2005 Read And Write A Sequential File
11 Aug, 2008 - 07:18 AM
Post #2

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 799



Thanked: 51 times
Dream Kudos: 2175
My Contributions
The problem is you are assuming that the file is in the correct format, never assume.
Test that is correct before using.
CODE

Option Explicit On
Option Strict On

Imports System.Convert
Imports System.IO

' The ContactReader class is used to read the
' sequential file containing the contacts.
Public Class RolodexList

    Private CurrentReader As StreamReader

    ' Create an instance of the StreamReader when the
    ' class instance is created.
    Public Sub New(ByVal argFile As String)
        CurrentReader = New StreamReader(argFile)
    End Sub

    ' Read the contact file.
    Public Function ReadContact() As Rolodex
        Dim CurrentContact As Rolodex
        Dim Fields() As String
        Dim CurrentRecord As String
dim Corrent_Size as integer=7
        ' Define the delimiter character.
        Dim DelimiterChars() As Char = {ToChar(",")}

        ' Read a line from the file.
        CurrentRecord = CurrentReader.ReadLine()

        ' Split the line into the respective fields.
        Fields = CurrentRecord.Split(DelimiterChars)
        if fields.length=Correct_Size Then

        ' Populate the contact record.
        With CurrentContact
            .lastName = Fields(0)
    .firstName = Fields(1) <-- My error
            .Address = Fields(2)
            .ContactDate = ToDateTime(Fields(3))
            .City = Fields(4)
            .State = Fields(5)
            .ZipCode = Fields(6)

        End With

        ' Return the contact record.
        Return CurrentContact
else
return Nothing
endif
    End Function


You'll need to check for nothing where if called.

User is offlineProfile CardPM
+Quote Post

bravo659
RE: VB.Net 2005 Read And Write A Sequential File
12 Aug, 2008 - 06:54 PM
Post #3

New D.I.C Head
*

Joined: 18 May, 2008
Posts: 32

I tried that and didnt work.
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: VB.Net 2005 Read And Write A Sequential File
13 Aug, 2008 - 03:05 PM
Post #4

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 799



Thanked: 51 times
Dream Kudos: 2175
My Contributions
QUOTE(bravo659 @ 13 Aug, 2008 - 03:54 AM) *

I tried that and didnt work.

Why? Give more information.

User is offlineProfile CardPM
+Quote Post

bravo659
RE: VB.Net 2005 Read And Write A Sequential File
13 Aug, 2008 - 03:23 PM
Post #5

New D.I.C Head
*

Joined: 18 May, 2008
Posts: 32

QUOTE(AdamSpeight2008 @ 13 Aug, 2008 - 04:05 PM) *

QUOTE(bravo659 @ 13 Aug, 2008 - 03:54 AM) *

I tried that and didnt work.

Why? Give more information.


What more information you need from the one I gave.
Is doesn't work with the coding and explanations you have given.
I need to send something here that you can understand what it is I am trying to accomplish, if I am allowed. I have an executable file that shows the actual program and I also have he txt files. Can I include into attachments?
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: VB.Net 2005 Read And Write A Sequential File
13 Aug, 2008 - 03:50 PM
Post #6

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,993



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
What he's asking is how did it not work? Did you get an error? What did it do versus what you need it to do? Saying It didn't work just doesn't give us much to work with smile.gif
User is offlineProfile CardPM
+Quote Post

bravo659
RE: VB.Net 2005 Read And Write A Sequential File
13 Aug, 2008 - 03:58 PM
Post #7

New D.I.C Head
*

Joined: 18 May, 2008
Posts: 32

QUOTE(AdamSpeight2008 @ 13 Aug, 2008 - 04:05 PM) *

QUOTE(bravo659 @ 13 Aug, 2008 - 03:54 AM) *

I tried that and didnt work.

Why? Give more information.


Oh sorry, I misinterpreted. I tried what you told me to include in the function return nothing, I did but it still renders the same previous error. "Index was outside the bounds of the array."
That was the previous error that made me seek for help in this forum. biggrin.gif
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: VB.Net 2005 Read And Write A Sequential File
13 Aug, 2008 - 04:09 PM
Post #8

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 799



Thanked: 51 times
Dream Kudos: 2175
My Contributions
This what meant by checking for nothing
CODE

dim rec as Rolodex=ReadContact
If IsNothing(rec) = True Then
' There as problem
Else
' Normal processing
End If

Added debug information to function
CODE


Option Explicit On
Option Strict On

Imports System.Convert
Imports System.IO

' The ContactReader class is used to read the
' sequential file containing the contacts.
Public Class RolodexList

    Private CurrentReader As StreamReader

    ' Create an instance of the StreamReader when the
    ' class instance is created.
    Public Sub New(ByVal argFile As String)
        CurrentReader = New StreamReader(argFile)
    End Sub

    ' Read the contact file.
    Public Function ReadContact() As Rolodex
        Dim CurrentContact As Rolodex
        Dim Fields() As String
        Dim CurrentRecord As String
dim Corrent_Size as integer=7
        ' Define the delimiter character.
        Dim DelimiterChars() As Char = {ToChar(",")}

        ' Read a line from the file.
        CurrentRecord = CurrentReader.ReadLine()

        ' Split the line into the respective fields.
        Fields = CurrentRecord.Split(DelimiterChars)
        if fields.length=Correct_Size Then

        ' Populate the contact record.
        With CurrentContact
            .lastName = Fields(0)
    .firstName = Fields(1) <-- My error
            .Address = Fields(2)
            .ContactDate = ToDateTime(Fields(3))
            .City = Fields(4)
            .State = Fields(5)
            .ZipCode = Fields(6)

        End With

        ' Return the contact record.
        Return CurrentContact
else
' Debug information
debug.print("Number of fields: {0}",fields.length)
return Nothing
endif
    End Function


This post has been edited by AdamSpeight2008: 13 Aug, 2008 - 04:15 PM
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: VB.Net 2005 Read And Write A Sequential File
13 Aug, 2008 - 04:18 PM
Post #9

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 799



Thanked: 51 times
Dream Kudos: 2175
My Contributions
I also reckon on that there is a blank line in the file,most likely the very last one.

This post has been edited by AdamSpeight2008: 13 Aug, 2008 - 04:19 PM
User is offlineProfile CardPM
+Quote Post

bravo659
RE: VB.Net 2005 Read And Write A Sequential File
13 Aug, 2008 - 05:18 PM
Post #10

New D.I.C Head
*

Joined: 18 May, 2008
Posts: 32

QUOTE(AdamSpeight2008 @ 13 Aug, 2008 - 05:18 PM) *

I also reckon on that there is a blank line in the file,most likely the very last one.


Ok, so far there is no error showing, but does not load to the textboxes.
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: VB.Net 2005 Read And Write A Sequential File
13 Aug, 2008 - 05:29 PM
Post #11

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 799



Thanked: 51 times
Dream Kudos: 2175
My Contributions
QUOTE(bravo659 @ 14 Aug, 2008 - 02:18 AM) *

QUOTE(AdamSpeight2008 @ 13 Aug, 2008 - 05:18 PM) *

I also reckon on that there is a blank line in the file,most likely the very last one.


Ok, so far there is no error showing, but does not load to the textboxes.

Debug information is displayed in the output window, it doesn't display a normal error box.

Textboxes? What textboxes? You've only just mentioned textboxes.

This post has been edited by AdamSpeight2008: 13 Aug, 2008 - 05:33 PM
User is offlineProfile CardPM
+Quote Post

bravo659
RE: VB.Net 2005 Read And Write A Sequential File
13 Aug, 2008 - 05:40 PM
Post #12

New D.I.C Head
*

Joined: 18 May, 2008
Posts: 32

QUOTE(AdamSpeight2008 @ 13 Aug, 2008 - 06:29 PM) *

QUOTE(bravo659 @ 14 Aug, 2008 - 02:18 AM) *

QUOTE(AdamSpeight2008 @ 13 Aug, 2008 - 05:18 PM) *

I also reckon on that there is a blank line in the file,most likely the very last one.


Ok, so far there is no error showing, but does not load to the textboxes.

Debug information is displayed in the output window, it doesn't display a normal error box.

Textboxes? What textboxes? You've only just mentioned textboxes.


The textboxes where the information is displayed. I have a form with 6 textboxes, one maskedtextbox, and a richtextbox. I mentioned that in my first post and part of my coding as well.

User is offlineProfile CardPM
+Quote Post

4 Pages V  1 2 3 > » 
Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 04:15PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month