Edit: I've found a work around, thanks for all your help :)
I'm working on a computer science project and I'm in the prototype stage right now. I will eventually switch this entire thing to a database once everything for it is sorted on the campus computers but for now I'm using text files. It's written on visual studio using VB (Required by my college). I'm trying to use the same text file in two different forms at different times but I keep getting the same error when I run it, saying that the file is open elsewhere. I assume this is because it reads from that file in the next form but I have made it so that the file isn't even opened until a specific button click on that other window. It reaches this error when I attempt to write data to the file from the first window, I never get to the stage where I read from the file in the next form.
Context:
It's a booking system for a youth hostel, the booking text file is written to, from the booking page and saved. This opens up a booking confirmation page where data is read from the last line of the file and necessary information is taken out (Name, checkin/checkout dates, booking reference etc.) to be inserted into the booking confirmation message so it's sort of tailored to each booking but keeps the same structure. I have a button to "save booking" which would write the data to the file but this is where the error occurs. The other form isn't open, the text file isnt open anywhere else and after writing to the file, it's closed.
Is there a workaround? should I just try using variables to hold the input data to use it in the next form? I'm not too worried about it being perfect because this is just a prototype until my college sorts out its issue with databases but I'd still like it to function the way I want it to.
Thank you :)
From booking page:
Private Sub btnBook_Click(sender As Object, e As EventArgs) Handles btnBook.Click
Dim NewBooking As Bookings
Dim errorFlag As Boolean
Dim bookingID As Integer
errorFlag = False
If txtID.Text = "" Or drpNumGuests.Text = "Please Select" Or txtCheckin.Text = "" Or txtCheckout.Text = "" Or txtRoomNo.Text = "" Then
errorFlag = True
End If
If errorFlag = True Then
MsgBox("Error with data entered, please try again.")
Else
Dim sw As New System.IO.StreamWriter("Bookings.txt", True) 'fix
Dim Booking() As String = File.ReadAllLines("bookings.txt")
If UBound(Booking) < 0 Then
bookingID = 0
Else
bookingID = Str(Int(Booking(UBound(Booking)).Substring(0, 8)) + 1) 'Automatic BookingID assignment
End If
NewBooking.GuestID = LSet(txtID.Text, 9)
NewBooking.BookingID = LSet(bookingID, 9)
NewBooking.Name = LSet(txtName.Text, 25)
NewBooking.GuestNum = LSet(drpNumGuests.Text, 2)
NewBooking.CheckIn = LSet(txtCheckin.Text, 11)
NewBooking.CheckOut = LSet(txtCheckout.Text, 11)
NewBooking.RoomNum = LSet(txtRoomNo.Text, 3)
NewBooking.Requests = txtRequests.Text
sw.WriteLine(NewBooking.GuestID & NewBooking.BookingID & NewBooking.Name & NewBooking.GuestNum & NewBooking.CheckIn & NewBooking.CheckOut & NewBooking.RoomNum & NewBooking.Requests)
FileClose()
MsgBox("Booking complete")
End If
End Sub
Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
Booking_Confirmation.Show()
Me.Hide()
From booking confirmation page:
Private Sub btnGen_Click(sender As Object, e As EventArgs) Handles btnGen.Click
Dim Bookconfirm() As String = File.ReadAllLines("bookings.txt")
Dim Name As String
Dim CheckIn As String
Dim CheckOut As String
Dim NumGuests As String
Dim RoomNum As String
Dim ID As String
Dim BookingID As String
Dim Count As Integer = UBound(Bookconfirm)
ID = Mid(Bookconfirm(Count), 1, 8)
BookingID = Mid(Bookconfirm(Count), 10, 8)
Name = Mid(Bookconfirm(Count), 19, 25)
CheckIn = Mid(Bookconfirm(Count), 46, 10)
CheckOut = Mid(Bookconfirm(Count), 57, 10)
NumGuests = Mid(Bookconfirm(Count), 44, 2)
RoomNum = Mid(Bookconfirm(Count), 68, 2)
txtConfirmation.Text = ID & " Thank you " & Name & " for booking your stay with us. Your booking of " & NumGuests & " has been made for " & CheckIn & "to " & CheckOut & "
We look forward to your arrival. Your booking ID is:" & BookingID
End Sub
Here's an alternate version you can try. As suggested by Hursey in the comments, a Using
statement will make sure the file is completely closed and released.
bookingID = 0
Dim lastLine As String = File.ReadAllLines("bookings.txt").LastOrDefault()
If Not IsNothing(lastLine) Then
bookingID = Str(Int(lastLine.Substring(0, 8)) + 1) 'Automatic BookingID assignment
End If
NewBooking.GuestID = LSet(txtID.Text, 9)
NewBooking.BookingID = LSet(bookingID, 9)
NewBooking.Name = LSet(txtName.Text, 25)
NewBooking.GuestNum = LSet(drpNumGuests.Text, 2)
NewBooking.CheckIn = LSet(txtCheckin.Text, 11)
NewBooking.CheckOut = LSet(txtCheckout.Text, 11)
NewBooking.RoomNum = LSet(txtRoomNo.Text, 3)
NewBooking.Requests = txtRequests.Text
Using sw As New System.IO.StreamWriter("Bookings.txt", True)
sw.WriteLine(NewBooking.GuestID & NewBooking.BookingID & NewBooking.Name & NewBooking.GuestNum & NewBooking.CheckIn & NewBooking.CheckOut & NewBooking.RoomNum & NewBooking.Requests)
End Using
MsgBox("Booking complete")
If all of the information is already in NewBooking
then either PASS that to the secondary form, or simply move it's declaration out to FORM level so that it's accessible. Then you won't need to read the file again to retrieve the information you literally just had?...