Question an error message appears

theazza

Member
Joined
May 20, 2011
Messages
23
Programming Experience
1-3
this code works but where it says
Dim
read As StreamReader = File.OpenText(FileNames(updatedSession))
an error message appears and i have no idea why
i also have to rewrite in to the text files each time a change has been done when the btnmove is pressed
can please be kind enough to debug it and finish it off beacause i have exams in week and dont have much time to go over it

THANKS


Imports
System.IO
Public Class JMASS
Dim folderPath As String = "C:\Session"
Dim FileNames() As String
Dim Sessions() As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim counter, i, j As Integer
Dim files As _
System.Collections.ObjectModel.ReadOnlyCollection(
Of String)
files =
My.Computer.FileSystem.GetFiles(folderPath) ' allows the program to get all the text files in the folder
Dim reader As StreamReader
Dim sessionName As String
counter = CStr(files.Count) 'determines how many text files there are in the folder
ReDim FileNames(0 To (counter - 1))
ReDim Sessions(0 To (counter - 1))
'specifies the program the get all the text files from the start to end of the folder
'If a training session (text file) is added into the sessions folder and program will be able to read all the text files, complying with the scope of the program
'Assigning 0 to i
i = 0
While (i < counter)
reader = File.OpenText(files.Item(i))
FileNames(i) = files.Item(i)
j = 0
sessionName =
""
'reads all the names in a particular text file
Do While j < 3
reader.Peek()
sessionName = sessionName +
" " + reader.ReadLine
j = j + 1
'reads the first 3 lines of each text file and ignores the rest
Loop
reader.Close()
cmbSession.Items.Add(sessionName)
Sessions(i) = sessionName
i = i + 1
'diplays the first three lines of each text file in the combo box
End While
End Sub
Private Sub lstSessions_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSession.SelectedIndexChanged
Dim selectedSession, i As Integer
selectedSession = cmbSession.SelectedIndex
'reads all the names in a particular text file and displays them in the listbox, except for the first first 3 lines
Dim reader As StreamReader = File.OpenText(FileNames(selectedSession))
lstNames.Items.Clear()
lstSessions.Items.Clear()
i = 0
Do While reader.Peek <> -1
If (i > 2) Then
lstNames.Items.Add(reader.ReadLine())
Else
reader.ReadLine()
End If
i = i + 1
Loop
reader.Close()
'Loads session names and absent list into a listbox giving the user the choice of moving a student to that class
Dim j As Integer
j = 0
Do While j < Sessions.Length
If (j <> selectedSession) Then
lstSessions.Items.Add(Sessions(j))
Else
lstSessions.Items.Insert(0, "Session Name")
End If
j = j + 1
Loop
End Sub
Private Sub lstupdate_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstSessions.SelectedIndexChanged
Dim updatedSession, i As Integer
updatedSession = lstupdate.SelectedIndex
Dim read As StreamReader = File.OpenText(FileNames(updatedSession))
lstupdate.Items.Clear()
i = 0
Do While read.Peek <> -1
If (i > 2) Then
lstupdate.Items.Add(read.ReadLine())
Else
read.ReadLine()
End If
i = i + 1
Loop
read.Close()
End Sub
Private Sub btnMove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMove.Click
Dim entry As String = lstNames.SelectedIndex
If lstupdate.Items.Count > 10 Then
MsgBox("Each lesson may only have 10 students")
End If
lstNames.Items.Remove(entry)
lstupdate.Items.Add(entry)

End Sub
End
Class
 
I hope this is not a final project or something for a programming class?

if u copy pasate it it appears as a single line on VB

What IDE are you using? I tried it with Visual Studio and it worked fine. If you download the zip file that solution should work without any problems, make sure that the textfiles are in the same directory as the executable, like if you are debugging the text files should be in the bin/debug folder and so forth..


can please be kind enough to debug it and finish it off beacause i have exams in week and dont have much time to go over it
:eek:I hope this is not a final project or something for a class? :( if it is i can only assist you... please debug it yourself, i'll be happy to help with the process, just be sure to post organized and exact questions.


Also please wrap your code that you paste into the posts inside of a Xcode or Codeblock
HTML:
[XCODE="vb"][/XCODE] or [CODE][/CODE]
Otherwise it is really hard to read and your spacing is all messed up... Like AsString should be two words not one... And there is no indenting.



about the code that you just posted, you said you are receiving an error message. Please post the error message. Looking at the code from a glance..

    Private Sub lstupdate_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstSessions.SelectedIndexChanged
        Dim updatedSession, i As Integer
        updatedSession = lstupdate.SelectedIndex

        'this line of code is where the error is?
        Dim read As StreamReader = File.OpenText(FileNames(updatedSession)) 'alt you can do this as well
        'Dim read As New StreamReader(FileNames(updatedSession))

        lstupdate.Items.Clear()
        i = 0
        Do While (read.Peek <> -1)
            'what is the purpose of this i integer? are you just trying to skip the first two lines that are read from the file?

            If (i > 2) Then
                lstupdate.Items.Add(read.ReadLine())
            Else
                read.ReadLine()
            End If
            i = i + 1 'can also be written
            'i +=1
        Loop
        read.Close()
    End Sub



if
Dim read As StreamReader = File.OpenText(FileNames(updatedSession))
is giving you an error, did you double check to make sure the correct file name and location is being retrieved from the array? I would set up a breakpoint in the debugger and see what value is passed in when the event is fired.


also in your load function, why are you converting the count from integer to a string when the count variable is a integer to being with?
Dim counter, i, j AsInteger
counter = CStr(files.Count)

'should just be..
Dim counter As Integer = files.Count


the code in that first little load section can be done without the ReDim and while if you are just trying to get the filenames into a string array

Dim files As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
files = My.Computer.FileSystem.GetFiles(folderPath)
Dim fileNames() As String = files.ToArray()
 
Back
Top