[.net] Error with my address book using VB.NET

Started by
1 comment, last by phil05 19 years ago
I'm getting this error: An unhandled exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll Additional information: Cannot read from a closed TextReader. Any idea why?

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sr As IO.StreamReader = IO.File.OpenText("Records.txt")
        Dim i, n As Integer  ' Counter 
        Dim name, street, city, state, zip, phone As String ' temp names

        n = 0 ' Number of records.

        Do While (sr.Peek <> -1)
            name = sr.ReadLine()
            street = sr.ReadLine()
            city = sr.ReadLine()
            state = sr.ReadLine()
            zip = sr.ReadLine()
            phone = sr.ReadLine() 

            n += 1  ' increment total records
        Loop
        sr.Close()

        Dim RecordEntry(n) As Entry

        Dim sr2 As IO.StreamReader = IO.File.OpenText("Records.txt")

        ' Read in data
        If (IO.File.Exists("Records.txt")) Then
            For i = 1 To n
                RecordEntry(i).Name = sr.ReadLine() ' ERROR HIGHLIGHTS HERE!
                RecordEntry(i).Street = sr.ReadLine()
                RecordEntry(i).City = sr.ReadLine()
                RecordEntry(i).State = sr.ReadLine()
                RecordEntry(i).Zip = sr.ReadLine()
                RecordEntry(i).Phone = sr.ReadLine()
            Next
        End If

        ' Display data in lstRecords
        For i = 1 To n
            lstRecords.Items.Add(RecordEntry(i).Name)
        Next

        sr2.Close()
    End Sub



Advertisement
I don't know vb that well but by looking at your code I would say it was this

sr.Close()

Dim RecordEntry(n) As Entry

Dim sr2 As IO.StreamReader = IO.File.OpenText("Records.txt")

' Read in data
If (IO.File.Exists("Records.txt")) Then
For i = 1 To n
RecordEntry(i).Name = sr.ReadLine() ' ERROR HIGHLIGHTS HERE!

You close sr and then make a sr2 but then try to use sr again...
Oh, okay. Thanks for pointing that out.

This topic is closed to new replies.

Advertisement