5 Replies - 1316 Views - Last Post: 01 June 2011 - 04:48 PM Rate Topic: -----

#1 dwins89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-March 11

Reading From A .txt File in VB

Posted 01 June 2011 - 02:18 PM

My assignment is a two part assignment, part 1 writing to a .txt file and the 2nd part is reading from that .txt file...I've done the writing part already, but I'm stuck in the reading the .txt file part..it doesn't display anything.

Imports System.IO
Public Class frmEmployeeData2
    Dim Open As OpenFileDialog
    Dim myFileReader As System.IO.StreamReader

    Private Sub btnNextRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextRecord.Click
        With ofdOpen
            .Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
            .InitialDirectory = "C:\ "
            .Title = "Open Files"
            .ShowDialog(Me)

            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                myFileReader = System.IO.File.OpenText(.FileName)
            End If


            lblDisplayName.Text = myFileReader.ReadLine().ToString
            lblDisplayMiddleName.Text = myFileReader.ReadLine().ToString
            lblDisplayLastName.Text = myFileReader.ReadLine().ToString
            lblDisplayEmployeeNum.Text = myFileReader.ReadLine().ToString
            lblDisplayDepartment.Text = myFileReader.ReadLine().ToString
            lblDisplayPhone.Text = myFileReader.ReadLine().ToString
            lblDisplayExtension.Text = myFileReader.ReadLine().ToString
            lblDisplayEmail.Text = myFileReader.ReadLine().ToString
            myFileReader.Close()


        End With
    End Sub

    Private Sub frmEmployeeData2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        With ofdOpen
            .Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
            .InitialDirectory = "C:\ "
            .Title = "Open Files"

            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                myFileReader = System.IO.File.OpenText(.FileName)
            End If

            lblDisplayName.Text = myFileReader.ReadLine().ToString
            lblDisplayMiddleName.Text = myFileReader.ReadLine().ToString
            lblDisplayLastName.Text = myFileReader.ReadLine().ToString
            lblDisplayEmployeeNum.Text = myFileReader.ReadLine().ToString
            lblDisplayDepartment.Text = myFileReader.ReadLine().ToString
            lblDisplayPhone.Text = myFileReader.ReadLine().ToString
            lblDisplayExtension.Text = myFileReader.ReadLine().ToString
            lblDisplayEmail.Text = myFileReader.ReadLine().ToString
            myFileReader.Close()


        End With
    End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Reading From A .txt File in VB

#2 smohd  Icon User is online

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Reading From A .txt File in VB

Posted 01 June 2011 - 02:44 PM

It works fine to me except you have to dot he following changes:
1. Remove the
.ShowDialog(Me) 
line, it re-opens the dialogbox
2. No need to start with System.IO while you have imported it before
EDIT: No use of this
Dim Open As OpenFileDialog
declaraton and then you have to take care of the lines in the text file if they exist

This post has been edited by smohd: 01 June 2011 - 02:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 dwins89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-March 11

Re: Reading From A .txt File in VB

Posted 01 June 2011 - 02:54 PM

Thats my program and it still doesn't work for some reason

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#4 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: Reading From A .txt File in VB

Posted 01 June 2011 - 03:43 PM

try putting your readlines() inside your if statement:

If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                myFileReader = System.IO.File.OpenText(.FileName)
            


            lblDisplayName.Text = myFileReader.ReadLine().ToString
            lblDisplayMiddleName.Text = myFileReader.ReadLine().ToString
            lblDisplayLastName.Text = myFileReader.ReadLine().ToString
            lblDisplayEmployeeNum.Text = myFileReader.ReadLine().ToString
            lblDisplayDepartment.Text = myFileReader.ReadLine().ToString
            lblDisplayPhone.Text = myFileReader.ReadLine().ToString
            lblDisplayExtension.Text = myFileReader.ReadLine().ToString
            lblDisplayEmail.Text = myFileReader.ReadLine().ToString
            myFileReader.Close()
End If

This post has been edited by Btu: 01 June 2011 - 03:45 PM

Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is online

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Reading From A .txt File in VB

Posted 01 June 2011 - 03:59 PM

Yap, there is nothing in the files that you are trying to read data from, may be you should feed data to them first.

This is caused because you are trying to save data in frmEmployeeData_Load event, which is not correct, since when the form loads there is no any data in the textboxes(that makes to create empty lines in the file).
try the following:
DO: remove any code from frmEmployeeData
DO: when buttonSave is clicked, create a new file and feed it with the data available from textboxes.(No need to use Save DialogBox)
Was This Post Helpful? 0
  • +
  • -

#6 dwins89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 13-March 11

Re: Reading From A .txt File in VB

Posted 01 June 2011 - 04:48 PM

ok ill try it out and post the results later on! thanks =]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1