Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This form allows me to calculate the monthly average when I click the respective button, I want to be able to calculate the average display it in a label and then save this value into a text file and when calculating another average I want to be able to display the previous value calculated previously saved into the text file. The error that I keep coming across is " An unhandled exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll,Additional information: Cannot read from a closed TextReader", I have tried doing some changes but I really don't know how to sort this out, has anyone got any ideas? please let me know and thanks in advance.

What I have tried:

Private Sub btnAverage_Click(sender As Object, e As EventArgs) Handles btnAverage.Click
        lblAverage2.Text = (Val(txtBoxWk1.Text) + Val(txtBoxWk2.Text) + Val(txtBoxWk3.Text) + Val(txtBoxWk4.Text)) / 4

        Dim Filenum As Integer = FreeFile()

        FileOpen(Filenum, "C:\Users\Windows 7 User\Desktop\Average.txt", OpenMode.Output)

        PrintLine(Filenum, lblSum.Text)

        FileClose(Filenum)
        MessageBox.Show("Current average has been calculated")





        Dim STUDENT_FILE As String = "C:\Users\Windows 7 User\Desktop\Average.txt"
        Dim objReader As New System.IO.StreamReader(STUDENT_FILE)

        Dim strDataline As String 'Data line
        Dim strArr(2) As String ' Array for bits of line
        Dim blfound As Boolean

        Do While objReader.Peek() <> -1 'read the file till the end.
            strDataline = (objReader.ReadLine()) ' read line and store into variable
            strArr = strDataline.Split(",") 'Split line and put into array

            label3.Text = strDataline


            blfound = True
            objReader.Close()

        Loop

        

    End Sub
Posted
Updated 2-Mar-18 9:27am
v2

1 solution

You could try something like this:
File.WriteAllText("C:\Users\Windows 7 User\Desktop\Average.txt", "blahdiblahblah")

Dim value As String = File.ReadAllText("C:\Users\Windows 7 User\Desktop\Average.txt")

See: File.WriteAllText Method (String, String) (System.IO)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900