# Appending/Overwriting Text File in VB 2008



## lildragon555 (Apr 1, 2008)

Hi, I'm trying to create an organizer for my robotics team and whenever I try to save to a file, it either doesn't save or crashes. Can anyone take a look and help?

Programming Language = VB 2008 or VB.Net...think they're the same


```
Public Class frmScouting
#Region "Variables"
    Dim writer As System.IO.StreamWriter
    Dim reader As System.IO.TextReader
    Dim teamnum As String
    Dim green As String
    Dim orangeT As String
    Dim orangeF As String
    Dim orangeW As String
    Dim white As String
    Dim autopoints As String
    Dim autoballs As String
    Dim speed As String
    Dim attachspeed As String
    Dim notes As String
#End Region
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
        lstResults.Items.Clear()

        teamnum = txtTeam.Text
        green = cmbGreen.Text
        orangeT = cmbOgT.Text
        orangeF = cmbOgFloor.Text
        orangeW = cmbOgWall.Text
        white = cmbWhite.Text
        autopoints = cmbAutoAmount.Text
        autoballs = cmbAutoBalls.Text
        speed = cmbSpeed.Text
        attachspeed = cmbAttach.Text
        notes = txtNotes.Text

        With lstResults.Items
            .Add("Team Number: " & teamnum)
            .Add("Green: " & green)
            .Add("Orange:")
            .Add("T: " & orangeT)
            .Add("Floor: " & orangeF)
            .Add("Wall: " & orangeW)
            .Add("White: " & white)
            .Add("Autonomous:")
            .Add("Amount of Points: " & autopoints)
            .Add("Types of Balls: " & autoballs)
            .Add("Speed: " & speed)
            .Add("Attach Speed: " & attachspeed)
            .Add("Notes: " & notes)
        End With
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim contents As String

        If System.IO.File.Exists(teamnum & ".txt") = False Then
            writer = New IO.StreamWriter(teamnum & ".txt")

            For i = 0 To lstResults.Items.Count - 1
                writer.WriteLine(lstResults.Items.Item(i))
            Next

        ElseIf System.IO.File.Exists(teamnum & ".txt") = True Then
            reader = System.IO.File.OpenText(teamnum & ".txt")
            contents = reader.ReadToEnd

            If InStr(contents, lstResults.Text) = 0 Then
                writer = IO.File.AppendText(teamnum & ".txt")
                writer.WriteLine(lstResults.Text)
            End If
        End If


        MsgBox("Successfully saved!")

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        End
    End Sub
End Class
```
Top half is just a lot of variables.


----------



## polishpaul (Oct 5, 2008)

run the debugger and see where it crashes, it should give you an unhandled exception error, most likely. The debugger is a tool just for solving this problem.


----------



## lildragon555 (Apr 1, 2008)

Well, I figured out what the problem was an forgot about this thread. Sorry.


----------



## polishpaul (Oct 5, 2008)

Well, why not post the solution?! Isn't that what a forum is for? :sigh:


----------



## lildragon555 (Apr 1, 2008)

XD well it's very inefficient, but I just copied the 1st part of the IF BLOCK to the 2nd part of the IF BLOCK...


----------

