8 Replies - 3859 Views - Last Post: 02 May 2012 - 12:28 AM Rate Topic: ***** 1 Votes

#1 Rhendorr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-April 12

Streamwriter and Structure help, 'Employee Data'.

Posted 19 April 2012 - 04:34 PM

Hello all! Long time lurker, first time poster. My assignment was a programming challenge in 'Starting Out with Visual Basic 2010' in which we were tasked with designing a form that would write employee information to a text file, such as name, employee, number, department, telephone, etc. The instructor has asked us to include an OpenFileDialog box asking the user where/which file to use as the application is generated. The instructor has also asked that we include and use a structure in the code, as well as placing the file close command in the code for the exit button. I have been going back and forth trying different things for some time now and will post what I have so far. I have commented out some code instead of erasing it just to try some things. Any hints or tips would be greatly appreciated, thanks in advance!

Imports System.IO

Public Class Form1
    Public Structure empdata
        Dim strFirstName As String
        Dim strMiddleName As String
        Dim strLastName As String
        Dim intEmpNumber As Integer
        Dim strDepartment As String
        Dim intPhone As Integer
        Dim intExt As Integer
        Dim strEmail As String
    End Structure
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' ofdEmployeeData.Filter = "EmployeeData.txt (*.txt)|*.txt"
        'If ofdEmployeeData.ShowDialog() = Windows.Forms.DialogResult.OK Then
        'Else
        ' MessageBox.Show("You have not selected a file.")
        ' End If
        cboDepartment.Items.Add("Accounting")
        cboDepartment.Items.Add("Administration")
        cboDepartment.Items.Add("Marketing")
        cboDepartment.Items.Add("MIS")
        cboDepartment.Items.Add("Sales")
    End Sub
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim info As empdata
        Dim txtFile As StreamWriter
        txtFile = File.AppendText("EmployeeData.txt")
        Try
            txtFile.WriteLine(txtFirst.Text)
            txtFile.WriteLine(txtMiddle.Text)
            txtFile.WriteLine(txtLast.Text)
            txtFile.WriteLine(txtEmpNum.Text)
            txtFile.WriteLine(cboDepartment.Text)
            txtFile.WriteLine(txtPhone.Text)
            txtFile.WriteLine(txtExtension.Text)
            txtFile.WriteLine(txtEmail.Text)
            txtFile.Close()
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtFirst.Clear()
        txtMiddle.Clear()
        txtLast.Clear()
        txtEmpNum.Clear()
        cboDepartment.SelectedIndex = -1
        txtPhone.Clear()
        txtExtension.Clear()
        txtEmail.Clear()
    End Sub
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        'txtFile.Close()
        Me.Close()
    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Streamwriter and Structure help, 'Employee Data'.

#2 Rhendorr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-April 12

Re: Streamwriter and Structure help, 'Employee Data'.

Posted 19 April 2012 - 05:17 PM

I have also tried,

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim info As empdata
        Dim txtFile As StreamWriter
        txtFile = File.AppendText("EmployeeData.txt")
        info.strFirstName = txtFirst.Text
        info.strMiddleName = txtMiddle.Text
        info.strLastName = txtLast.Text
        info.intEmpNumber = txtEmpNum.Text
        info.strDepartment = cboDepartment.Text
        info.intPhone = txtPhone.Text
        info.intExt = txtExtension.Text
        info.strEmail = txtEmail.Text
        Try
            txtFile.WriteLine(info.strFirstName)
            txtFile.WriteLine(info.strMiddleName)
            txtFile.WriteLine(info.strLastName)
            txtFile.WriteLine(info.intEmpNumber)
            txtFile.WriteLine(cboDepartment.Text)
            txtFile.WriteLine(info.intPhone)
            txtFile.WriteLine(info.intExt)
            txtFile.WriteLine(info.strEmail)
            txtFile.Close()
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub


but I keep getting an error if there is text instead of an integer in any of the int spaces (cant get a CInt() to work). would it be better to use something like:

Try
            info.strFirstName = txtFile.WriteLine(txtFirst.Text)



or would that even work?
again, any info or tips are greatly appreciated. Thanks in advance!!!
Was This Post Helpful? 0
  • +
  • -

#3 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: Streamwriter and Structure help, 'Employee Data'.

Posted 19 April 2012 - 05:35 PM

Have you tried Integer.Parse or Integer.TryParse? (hint: MSDN)
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,058
  • Joined: 02-July 08

Re: Streamwriter and Structure help, 'Employee Data'.

Posted 19 April 2012 - 06:10 PM

Dim info As empdata


Should be

Dim info As New empdata


This initializes the object. You should also dispose your StreamWriter in a Finally block if you use a Try/Catch, and not before the Catch in case it explodes before you close the stream.
Was This Post Helpful? 0
  • +
  • -

#5 Rhendorr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-April 12

Re: Streamwriter and Structure help, 'Employee Data'.

Posted 19 April 2012 - 06:16 PM

Dimitri,
I tried the tryparse and was still coming back with errors. Maybe I was doing it wrong?

Hawk,
I will make those changes. However, I am not familiar with 'finally blocks'? any suggestions where I can read into that?
Was This Post Helpful? 0
  • +
  • -

#6 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: Streamwriter and Structure help, 'Employee Data'.

Posted 19 April 2012 - 06:19 PM

Or... use a Using statement. That takes care of it all for you.
Using sw As StreamWriter = FIle.AppendText("foo.txt")
'do stuff here
/* do stuff here */
End Using



@Rhendor - enter down after you have typed Catch ex As Exception, type Finally, and then on a new line below that type sw.Close() and on a new line sw.Dispose().
Look here for more info:
http://msdn.microsof...z(v=vs.80).aspx

This post has been edited by DimitriV: 19 April 2012 - 06:20 PM

Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,058
  • Joined: 02-July 08

Re: Streamwriter and Structure help, 'Employee Data'.

Posted 19 April 2012 - 07:30 PM

Try/Catch/Finally
Was This Post Helpful? 0
  • +
  • -

#8 Rhendorr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-April 12

Re: Streamwriter and Structure help, 'Employee Data'.

Posted 19 April 2012 - 09:02 PM

Thanks guys! I'll read over these and give it another shot this weekend. I wont have a lot of time til Sunday, but I will post back if I have anymore problems. Thanks!!
Was This Post Helpful? 0
  • +
  • -

#9 Rhendorr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-April 12

Re: Streamwriter and Structure help, 'Employee Data'.

Posted 02 May 2012 - 12:28 AM

I just wanted to reply back and say thanks for your help guys! My instructor gave me full credit, because technically the integer in the structure was working, just coming back with an error if it was a character entered other than an integer. He says in fields such as telephone number, and employee number, etc, that it would be okay to go ahead and run those as strings as well, since you should never need those numbers for any sort of adding or subtracting.
Anyways, thanks again, I really appreciate it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1