masj78 Posted August 20, 2003 Posted August 20, 2003 I am using a file as a data store. It will allow me to write one word or sentance but if I try to write more than one it gives me my error opening file. Code is as follows: VB.NET: Dim strStore As String Dim fileName As New IO.StreamWriter(File.Open("C:\store.txt", IO.FileMode.Append)) ----------------------------------------------------------------------------------- Private Sub cmdStore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStore.Click Try strStore = storeText.Text ' Text from text box fileName.WriteLine(System.Environment.NewLine) fileName.WriteLine(strStore) ' write to stream MsgBox(strStore & " has been stored!") ' message has stored fileName.Flush() ' flush to file fileName.Close() ' close file fileName = Nothing Catch ex As Exception MsgBox("Error Opening File!") 'error that is showing on second store attempt End Try End Sub Have a vague idea its something to do with opening and closing the file. Any ideas of the solution anyone!:confused: Quote
*Experts* mutant Posted August 20, 2003 *Experts* Posted August 20, 2003 It would be better if you did it without error handling once so you could show us where it throws the error. Quote
Administrators PlausiblyDamp Posted August 20, 2003 Administrators Posted August 20, 2003 fileName.WriteLine(System.Environment.NewLine) fileName.WriteLine(strStore) ' write to stream The first of the above lines may be redundant, .WriteLine should write out the data and include an appropriate line terminator (e.g. CRLF) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
masj78 Posted August 20, 2003 Author Posted August 20, 2003 I have debugged as suggested and the line causing the problem is : fileName.WriteLine(System.Environment.NewLine) The error is: A first chance exception of type 'System.NullReferenceException' occurred in WordMatcher.exe Additional information: Object reference not set to an instance of an object. If this line is commented out it gives the same error, but on the line below: fileName.WriteLine(strStore) Intead of closing the file after the word is added I have put it when the program is exited and it seems to have worked. Would like some explanation on the error, If possible! Quote
Administrators PlausiblyDamp Posted August 20, 2003 Administrators Posted August 20, 2003 try changing Dim fileName As New IO.StreamWriter(File.Open("C:\store.txt", IO.FileMode.Append)) to Dim fileName As New IO.StreamWriter(File.Open("C:\store.txt", IO.FileMode.Append, FileAccess.Write)) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
masj78 Posted August 20, 2003 Author Posted August 20, 2003 Changing/adding parameters seems to make no difference. It seems that you need to keep the file open, do all your editing, then close it. Doesn't like the file being constantly open and closed at run time it seems. Have learnt a lot so far and at least I have a solution. cheers for al the help everyone! For anyone interested, the solution I found was to add this command on the streamwriter on the exit button. Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click fileName.Close() ' close file fileName = Nothing End End Sub Quote
Administrators PlausiblyDamp Posted August 20, 2003 Administrators Posted August 20, 2003 Or if you open the File at the start of the function and close it at the end - just realised you were opening it outside the function (soon as the form is loaded into memory by the look of it) and closing it when the function exits. The next time the function is called the file will be closed. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders dynamic_sysop Posted August 20, 2003 Leaders Posted August 20, 2003 your best bet would be to build up the string you want to write, then use writeline in one go, eg: strStore = "some stuff" strStore += Environment.NewLine strStore += "some more stuff" fileName.WriteLine(strStore) also, have you tried using just " Write " rather than " WriteLine "? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.