FileOpen, PrintLine not working
-
I have a project that gets a name from a listbox, and a date from a DateTimePicker
I then read a file called "All Appts.txt" I check the file for matching criteria that I got from the listbox and datepicker..... If I find matching data ... then store the whole line in variables I have set.
This is the Code:
Private Sub ApptFileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ApptFileButton.Click 'declare any variables we may need Dim strFirstName, strLastName, strDoctorName As String Dim dteDate As DateTime Dim dtetime As DateTime 'try to open All Appts.txt 'prevent program crash on invalid file Try FileOpen(2, "All Appts.txt", OpenMode.Input) Catch ex As Exception MessageBox.Show("Either the file All Appts.txt does not exist or has been moved.") End Try 'begin reading All Appts.txt and load the data into the variables 'that we declared. Try Do While Not EOF(2) 'if statement to get data I want 'if data = doctor from the listbox and the date I read = datetimepicker1.value.date 'then write data to file Input(2, strFirstName) Input(2, strLastName) Input(2, dteDate) Input(2, dtetime) Input(2, strDoctorName) 'If we found matching data in All Appts.txt If strDoctorName = DoctorListBox.SelectedItem And dteDate = DateTimePicker1.Text Then 'Build The Report Dim strReportName As String strReportName = strDoctorName & " Appts For " & dteDate & ".txt" FileOpen(3, strReportName, OpenMode.Append) PrintLine(3, "Jason Bilow") PrintLine(3, ControlChars.NewLine) PrintLine(3, ControlChars.NewLine) PrintLine(3, "Name", TAB(20), "Time") PrintLine(3, strFirstName, strLastName, TAB(20), dtetime) End If Loop FileClose(2) PrintLine(3, "End Of Appointment List") FileClose(3) Catch MessageBox.Show("the record you asked for was not found") End Try
When using breakpoints, this code works fine until the line that I have highlighted in red. strReportName builds the name of the file I want to create and gets stored in strReportName. As soon as I hit the line that is highlighted in red, hit F11 to execute it, my program immediately jumps to the catch code.
Why is this? What have I done wrong?
-
Well the most logical guess is that the file 'strReportName' points to can't be accessed (ie. it's already open). I believe the reason might be due to the fact that when you find a record you open the file but never close it. If another matching record is found you once again try to open the file which throws an exception. Try opening the file before the if statement. This way it's only opened once.
The catch portion of your try...catch block should look like this. 'Catch ex as exception'. The variable 'ex' contains a number of properties. One of them is 'message'. I believe if you display that you'll see the error that is occuring which should help you diagnose exactly what the problem is. Your other option is to comment out the try...catch block so that the error gets thrown.
Also the catch portion of the try...catch block is to catch exceptions. These are errors that are thrown that would normally crash your program. You've got this code in your catch block.
MessageBox.Show("the record you asked for was not found")
This doesn't make any sense because the record not being found will NOT throw an exception. It might be more helpfull to 1) Display the actual error message 2) Capture specific errors instead of using a single catch all.
A try...catch block might look something like this.
Try FileOpen(2, "c:\test", OpenMode.Append) Catch ex As System.IO.IOException ' Catches problems that might occur when accessing a file Catch ex As Exception ' Catches all other exceptions we didn't/couldn't plan for End Try
Honestly for your code you might be fine with a single catch all, but under different circumstances catching specific exceptions might be the way to go. Plus it will help you distinguish different errors so you can act appropriatly.
If a record isn't found the file would have never been opened and PrintLine(3, "End Of Appointment List") will throw an error. Come to think of it this is probably the only error you were planning for, thus the record couldn't be found message. Like I said a record not being found doesn't throw an error. The error was occuring because the file was never opened as a result of the record not being found. Your message was planned around the possibility of this one exception. As you've found out other errors may occur that are unrelated to the record not being found. Perhaps you should use a boolean and set it to true when a record is found. Then you can test the boolean and if a record was found write the last line to the file, if not display a message that the record was not found. This way your not relying on exceptions for flow control.
One last thing. It may not be worth it to change your code now but FileOpen was left over from vb6 I believe. The new way to read/write files is to use the StreamReader and StreamWriter classes. You should look into using those in the future.
Post a reply
Quick links
Recent activity
- arif ahmad replied to How to receive data in web ...
- William Thompson replied to What is the name of the Win...
- Sameera Piyadigamage replied to Point of Sale Developers: H...
- Scott Carline replied to 4 x C# Developers for large...
- Rajendra Dhakal replied to Restore SQL Server text dat...
- cloud rainda replied to How to convert between TS f...
Enter your message below
Sign in or Join us (it's free).