• Advertisement
Sign in to follow this  

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

This topic is 4685 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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



Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement