views:

26

answers:

1

I am new to visual basic and I am just playing around with it. I have a book that tells me how to read from a file but not how to write to the file with a button click. All I have is a button and a textbox named fullNameBox. When I click the button it gives me an unhandled exception error. Here is my code:

Public Class Form1
    Sub outputFile()
        Dim oWrite As System.IO.StreamWriter
        oWrite = System.IO.File.CreateText("C:\sample.txt")
        oWrite.WriteLine(fullNameBox.Text)
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        outputFile()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
+1  A: 

Have you tried stepping through your application to see where the error is? With a quick glance, it looks like you might need to use System.IO.File on the fourth line (oWrite = IO.File...) instead of just IO, but I haven't tried to run it.

cincodenada
I changed it, but it still didnt work. The program gives me no errors until I click the button then it gives me the unhandled exception error
shinjuo
Have you tried putting a breakpoint in the function and then stepping through to see what line is causing the problem? "Unhandled Exception" is not a very helpful error, it just means something went wrong that you didn't account for. Stepping through it should give you more information.
cincodenada