Reputation: 1197
I have a form with fields that i am saving to the text file.
On that same form I have a button that allows you to view what you saved prior from that text file
When i hit the button it reads the textfile and displays on a datagrid.
This code is what is displaying to the datagrid
Dim reader As StreamReader
reader = New StreamReader("C:\Users\John\Desktop\tickets.txt")
Dim a As String
Dim results() As String
Do
a = reader.ReadLine
results = a.Split(vbTab)
dgv.Rows.Add(results(0), results(1), results(2), results(3), results(4), results(5))
' Code here
'
Loop Until a Is Nothing
reader.Close()
reader.Dispose()
this code saves to the file
Dim file As StreamWriter
file = New StreamWriter("C:\Users\John\Desktop\tickets.txt", True)
file.WriteLine(txtname.Text & vbTab & dtdate.Text & vbTab & txttime.Text & vbTab & txtwho.Text & vbTab & txtproblem.Text & vbTab & txtresolution.Text)
file.Close()
When I read the records first then try to save them I keep getting the error the process cannot access the file because it is being used by another process.
I closed both the reader and writer, and am not sure why this is happeneing
Upvotes: 0
Views: 726
Reputation: 9024
Better code, are you sure your loop was ending?
Write:
Dim path As String = "C:\Users\John\Desktop\tickets.txt"
If File.Exists(path) Then
Using sw As New StreamWriter(path, True)
'code
End Using' closes and disposes for you
End If
Read:
Dim path As String = "C:\Users\John\Desktop\tickets.txt"
If File.Exists(path) Then
Dim results As New List(Of String)
Using sr As New StreamReader(path)
While Not sr.EndOfStream()
results.Add(sr.Readline())
'better than do loop
End While
End Using' closes and disposes for you
End If
For Each line In results
'add to DGV
Next
Upvotes: 1