Hello all
I'm learning VB and I've hit a road block. I've tried looking in books and searching the web but I can't seem to find an answer at all. I've been trying to get the streamreader to read my text file and display it in a list box. Hever it keeps saying that itscannot read the file. Now it will put the "Student" & studentCount + 1 inf the list box. Where am I going wrong here?

Imports System.IO
Public Class GradeRepot
    Dim fileWriter As StreamWriter
    Dim result As DialogResult
    Dim fileName As String
    Dim grades(2, 2) As Integer
    Dim studentCount As Integer = 0



    Private Sub GradeReport_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        gradeListBox.Items.Add(vbTab & vbTab & vbTab & "Test 1" & vbTab & "Test 2" & vbTab & "Test 3")
        CloseToolStripMenuItem.Enabled = False
        OpenToolStripMenuItem.Enabled = False

    End Sub

    Sub CloseFile()
        If fileWriter IsNot Nothing Then
            Try
                fileWriter.Close()
            Catch ex As IOException
                MessageBox.Show("Error closing file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub

    Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewToolStripMenuItem.Click
        CloseFile()

        Using fileChooser As New SaveFileDialog
            result = fileChooser.ShowDialog()
            fileName = fileChooser.FileName
        End Using

        If result <> Windows.Forms.DialogResult.Cancel Then
            Try
                fileWriter = New StreamWriter(fileName, True)
                CloseToolStripMenuItem.Enabled = True
                inputGroupBox.Enabled = True
                submitButton.Enabled = True
                readGradeListBox.Enabled = False

            Catch ex As IOException
                MessageBox.Show("Error Opening File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub

    Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
        Dim result As DialogResult


        Using fileChooser As New OpenFileDialog()
            result = fileChooser.ShowDialog()
            fileName = fileChooser.FileName
        End Using
        If result <> Windows.Forms.DialogResult.Cancel Then
            Try
                fileWriter = New StreamWriter(fileName, True)
                CloseToolStripMenuItem.Enabled = True
                inputGroupBox.Enabled = False
                submitButton.Enabled = False
                readButton.Enabled = True
                readGradeListBox.Enabled = True
            Catch ex As IOException
                MessageBox.Show("Erroe Opening File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub

    Private Sub CloseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CloseToolStripMenuItem.Click
        CloseFile()
        CloseToolStripMenuItem.Enabled = False
        NewToolStripMenuItem.Enabled = False
        inputGroupBox.Enabled = False
        submitButton.Enabled = False
        readButton.Enabled = False
        readGradeListBox.Enabled = False
        OpenToolStripMenuItem.Enabled = True
    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        CloseFile()
        Application.Exit()
    End Sub





    Private Sub submitButton_Click(sender As Object, e As EventArgs) Handles submitButton.Click
        grades(studentCount, 0) = Convert.ToInt32(test1TextBox.Text)
        grades(studentCount, 1) = Convert.ToInt32(test2TextBox.Text)
        grades(studentCount, 2) = Convert.ToInt32(test3TextBox.Text)

        Dim output As String = "Student " & studentCount + 1 & vbTab


        For column = 0 To grades.GetUpperBound(1)
            output &= vbTab & grades(studentCount, column)
        Next
        gradeListBox.Items.Add(output)

        studentCount += 1

        fileWriter.WriteLine(test1TextBox.Text & "," & test2TextBox.Text & "," & test3TextBox.Text)
        test1TextBox.Clear()
        test2TextBox.Clear()
        test3TextBox.Clear()
        test1TextBox.Focus()

        If studentCount = grades.GetUpperBound(0) + 1 Then
            inputGroupBox.Enabled = False
        End If
    End Sub

    Private Sub readButton_Click(sender As Object, e As EventArgs) Handles readButton.Click
        Dim fileReader As StreamReader = Nothing

        Try
            readGradeListBox.Items.Add("Student " & (studentCount + 1) & vbCrLf)

            fileReader = New StreamReader(fileName)

            Do While Not fileReader.EndOfStream
                Dim line As String = fileReader.ReadLine()
                Dim fields() As String = line.Split(","c)
                Dim test1 As String = fields(0)
                Dim test2 As String = fields(1)
                Dim test3 As String = fields(2)
            Loop

        Catch ex As IOException
            MessageBox.Show("Cannot Read File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Finally
            If fileReader IsNot Nothing Then
                Try
                    fileReader.Close()
                Catch ex As IOException
                    MessageBox.Show("Error Closing File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try
            End If
        End Try
    End Sub




End Class

Recommended Answers

All 3 Replies

on my opinion check the fileName variable does contain "Full Path" of the file to open or not.

commented: For overall knowledge and effort +15

I did. I'm a trouble shooter so I had it print the name of the file in the list boxto ensure it was loading the right thing in fileName

You could never open a same file with two different instance.
In OpenToolStripMenuItem Click Event you already opened it as StreamWriter. How could you re-open a opened file as StreamReader in readButton Click Event. Think and clear your logic what did you want to do by open and read buttons.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.