1 Replies - 497 Views - Last Post: 25 March 2012 - 08:09 PM Rate Topic: -----

#1 GottaDoWork  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-March 12

Reading and Writing Lines to and from .txt file?

Posted 25 March 2012 - 08:06 PM

Hi, I'm fairly new to visual basic and I'm writing a program where there are text boxes and the user can enter info and append them to a text file as well as read from the text file and view the various entries in it. The problems I'm running into are that I have a button that opens an open file dialog box allowing you to select the file, and I want to get it to open the first entry of that file in the correct text boxes. the code so far looks like this

        
Dim result As DialogResult = OpenFileDialog.ShowDialog()

        OpenFileDialog.InitialDirectory = "G:\INFO 250\GoldenJohnHW1\GoldenJohnHW1\bin\Debug"
        ' open specified file if user did not click cancel button
        If result <> Windows.Forms.DialogResult.Cancel Then
            ' get specified file name
            Dim fileName As String = OpenFileDialog.FileName
            'load the text file and populate list box with options
            Dim lineReader As New System.IO.StreamReader(fileName)
            While (lineReader.Peek <> -1)
                peekLstBx.Items.Add(lineReader.ReadLine.Split(",")(0))
                lineReader.ReadLine()
            End While
        End If
    End Sub



I used to have this bit of code in there but there was a conflict with the .txt file being used more than once at the same time while it was in there.

            If CheckValidity(fileName) = True Then
                output = New StreamWriter(fileName, False)
            End If



Im also having trouble writing to the file and I'm not sure why. I have an "Append" button that has this code in it

        ' use streamwriter to write data to file
        output.Write(vendorNameTxtBx.Text & ControlChars.Tab)
        output.Write(phoneNumberTxtBx.Text & ControlChars.Tab)
        output.Write(streetAddress1TxtBx.Text & ControlChars.Tab)
        output.Write(streetAddress2TxtBx.Text & ControlChars.Tab)
        output.Write(cityTxtBx.Text & ControlChars.Tab)
        output.Write(stateTxtBx.Text & ControlChars.Tab)
        output.Write(zipCodeTxtBx.Text & ControlChars.Tab)
        output.Write(currentOutstandingBillTxtBx.Text & ControlChars.Tab)
        output.Write(salesTaxPayableTxtBx.Text & ControlChars.Tab)
        output.Write(salesYearToDateTxtBx.Text & ControlChars.Tab)
        output.Write(salesRepresentativeNameTxtBx.Text & ControlChars.Tab)
        output.WriteLine(commentsTxtBx.Text)



and it comes up with the issue "object reference not set to to an object instance". Any help would be much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading and Writing Lines to and from .txt file?

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5388
  • View blog
  • Posts: 27,385
  • Joined: 10-May 07

Re: Reading and Writing Lines to and from .txt file?

Posted 25 March 2012 - 08:09 PM

** Duplicate post **
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1