Search code examples
.netvb.netvisual-studio-debugging

VB.Net Save button not adding data to text file


I'm still new to VB.Net and have been stuck on this for a little over an hour. I'm trying to create an inventory management system where a form opens up for the user to enter pc specs and when I click save will save to the text file, so that it is read into the Inventory Tracker screen. Where each text box is disabled and appends the text as it is updated.

Currently nothing is happening, I've created the text file in the same directory as the project. Why is this?

I also tried putting a test message in the text file to display in the tracker text box, which is what will happen when I save the specs, however no message is appearing?

frmItemEntry.vb

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

    Dim objMyStreamReader As System.IO.StreamReader
    Dim objMyStreamWriter As System.IO.StreamWriter = System.IO.File.CreateText("inventory.txt")

    Dim strInventory As String

    objMyStreamWriter.WriteLine(txtManufacturerEntry)
    objMyStreamWriter.WriteLine(txtProcessorEntry)
    objMyStreamWriter.WriteLine(txtVideoEntry)
    objMyStreamWriter.WriteLine(txtFormEntry)
    objMyStreamWriter.WriteLine(txtRamEntry)
    objMyStreamWriter.WriteLine(txtVramEntry)
    objMyStreamWriter.WriteLine(txtHdEntry)
    objMyStreamWriter.WriteLine(chkWirelessEntry)
    objMyStreamWriter.Close()

    Me.Close()

End Sub

frmTracker.vb

Private Sub txtManufacturer_TextChanged(sender As Object, e As EventArgs) Handles txtManufacturer.TextChanged
    Dim streamReader As New System.IO.StreamReader("inventory.txt")
    Dim strInventory As String
    streamReader = System.IO.File.OpenText("inventory.txt")
    strInventory = streamReader.ReadLine
    txtManufacturer.AppendText(strInventory)
End Sub

Solution

  • Are you definitely looking at the right file?

    When you build and run a .NET project in VS it compiles the code into a sub-folder under your project (normally bin\Debug in you're in Debug mode) and also copies all the associated artefacts there, such as text files - similar to if you were deploying the final executable to the place where it will run for real. This becomes the root directory for the program while it's running.

    I would expect that it's actually updating a copy of the file in that sub-directory, not in the folder where your source code lives.