6 Replies - 2667 Views - Last Post: 25 March 2012 - 09:52 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:05 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 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1052
  • View blog
  • Posts: 4,077
  • Joined: 02-July 08

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

Posted 25 March 2012 - 09:14 PM

I am confused, you are reading from the file and splitting on a comma. Then writing to a file that is basically Tab separated? Are we talking about the same file or several?
Was This Post Helpful? 0
  • +
  • -

#3 GottaDoWork  Icon User is offline

  • New D.I.C Head

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

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

Posted 25 March 2012 - 09:29 PM

Im sorry that was from earlier, its supposed to be "," instead of ControlChar.Tab
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1052
  • View blog
  • Posts: 4,077
  • Joined: 02-July 08

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

Posted 25 March 2012 - 09:32 PM

Ok so where does that error come from - what line? Are missing some other relevant code?
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

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

Posted 25 March 2012 - 09:35 PM

Tip: Never hard code absolute filepath references.
Was This Post Helpful? 0
  • +
  • -

#6 GottaDoWork  Icon User is offline

  • New D.I.C Head

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

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

Posted 25 March 2012 - 09:45 PM

I know its kinda hard to tell whats going on ive been adding and removing code and trying to get stuff to work but pretty much the first bit of code used to look 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
            If CheckValidity(fileName) = True Then
                output = New StreamWriter(fileName, False)
            End If
        End If
    End Sub



I then tried to add
            Dim lineReader As New System.IO.StreamReader(fileName)
            While (lineReader.Peek <> -1)
                peekLstBx.Items.Add(lineReader.ReadLine.Split(",")(0))
            End While


so that it would populate a listbox with the different lines in the .txt file. the problem with that is that i cant have the file open to read and to write to at the same time and im not sure what to change to fix that or if i should even bother with the file dialog box since the program is meant to use only one .txt file.

The writing part of my problem is that the code
                output = New StreamWriter(fileName, False)


is meant to enable the file to be written to, so then all i need for the "Append" button is
        output.Write(vendorNameTxtBx.Text & ",")
        output.Write(phoneNumberTxtBx.Text & ",")
        output.Write(streetAddress1TxtBx.Text & ",")
        output.Write(streetAddress2TxtBx.Text & ",")
        output.Write(cityTxtBx.Text & ",")
        output.Write(stateTxtBx.Text & ",")
        output.Write(zipCodeTxtBx.Text & ",")
        output.Write(currentOutstandingBillTxtBx.Text & ",")
        output.Write(salesTaxPayableTxtBx.Text & ",")
        output.Write(salesYearToDateTxtBx.Text & ",")
        output.Write(salesRepresentativeNameTxtBx.Text & ",")
        output.WriteLine(commentsTxtBx.Text)


but thats when the error occurs about "object reference not set to to an object instance".

I guess some relevant code i have is up at the top with
    Private output As StreamWriter



but other than that i dont know what would be affecting it. i dont know if this helped clear up the problem or not.
Was This Post Helpful? 0
  • +
  • -

#7 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

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

Posted 25 March 2012 - 09:52 PM

Well yeah, the problem is with the StreamWriter declaration. The class requires you to instantiate it before you can use it and that means declaring it like so:
Private output As New StreamWriter(path)

But yes, AdamSpeight2008 is right. Never hard code file paths.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1