Results 1 to 6 of 6

Thread: [2005] output to file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    28

    Unhappy [2005] output to file

    I am having a hard time why my code does not write to a file.
    Code:
    Private Sub btnEnter_Click(ByVal sender As System.Object, _
                                   ByVal e As System.EventArgs) _
                                   Handles btnEnter.Click
    
            Dim values As String() = GetTextBoxValues()
    
            Dim record As New GradeBookDataCollector()
    
            Me.SetTextBoxValues(GetTextBoxValues)
            If values(TextBoxIndices.txtFirstName) <> "" Then
    
                Try
                    Dim strFirstName As String = _
                        values(TextBoxIndices.txtFirstName)
    
                    If strFirstName <> "" Then
                        record.strFirstName = values(TextBoxIndices.txtFirstName)
                        record.strLastName = values(TextBoxIndices.txtLastName)
                        record.strCourseName = values(ComboBoxIndices.cboCourseName)
                        record.strStudentId = values(TextBoxIndices.txtStudentId)
                        record.strGrade = values(ComboBoxIndices.cboGrade)
    
                        fileWriter.WriteLine( _
                        record.strFirstName.ToString & "," & record.strLastName.ToString & "," & _
                        record.strStudentId.ToString & "," & record.strCourseName.ToString & "," & _
                        record.strGrade.ToString)
    
    
                    Else
                        MessageBox.Show("Invalid First Name", "Error", _
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
    
                Catch ex As IOException
                    MessageBox.Show("Error Writing to File", "Error", _
                    MessageBoxButtons.OK, MessageBoxIcon.Error)
    
                Catch ex As FormatException
                    MessageBox.Show("Invalid Format", "Error", _
                   MessageBoxButtons.OK, MessageBoxIcon.Error)
    
                End Try
            End If
    
            Me.SetTextBoxValues(GetTextBoxValues)
            Me.ClearFormDataEntryBoxes()
    
        End Sub

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] output to file

    Where are you creating the fileWriter object? Have you stepped through your code to see what happens?

    And you must turn Option Strict ON and fix the errors you get.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] output to file

    Also, you've already asked this question in another thread. Do NOT post the same question more than once. If you have more information to post then post it to the existing thread, NOT a new one.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    28

    Unhappy Re: [2005] output to file

    I had a different question from the same program. Am I supposed to continue with the same thread for a different question?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] output to file

    This thread contains a chunk of code that also appears above. So are you saying that you've fixed that NullReferenceException that you were getting? I must have missed where you posted back to that other thread to say so.

    If you don't get a NullReferenceException anymore, what actually does happen? Also, 'fileWriter' is obviously important here so it would be useful to see where it's created. Also, you obviously open fileWriter somewhere else so do you actually close it somewhere else too? If not then the data won't be flushed to the file until you close the app.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Posts
    28

    Cool Re: [2005] output to file

    I forgot that I needed to create the file via a save as which is why there was an error when I did the enter button event to do the null method.

    Code:
    Public Class frmGradeBook
    
        Protected TextBoxCount As Integer = 3
        Protected ComboBoxCount As Integer = 2
        Private fileWriter As StreamWriter
        Private output As FileStream
    
        Public Enum TextBoxIndices
            txtFirstName
            txtLastName
            txtStudentId
        End Enum
    
        Public Enum ComboBoxIndices
    
            cboCourseName
            cboGrade
    
        End Enum
    
        ' Clear Text boxes once record saved.
        Public Sub ClearFormDataEntryBoxes()
    
            For i As Integer = 0 To Controls.Count - 1
    
                Dim myControl As Control = Controls(i)
    
                If TypeOf myControl Is TextBox Then _
                myControl.Text = ""
    
                If TypeOf myControl Is ComboBox Then _
                myControl.Text = Nothing
    
            Next
    
        End Sub
    
        Public Sub SetTextBoxValues(ByVal values() As String)
    
            For i As Integer = 0 To Controls.Count - 1
    
                Dim myControl As Control = Controls(i)
    
                If TypeOf myControl Is TextBox And _
                          myControl.Text = "" Then _
                          ErrorMessage1() Else 
    
                If TypeOf myControl Is ComboBox And _
                          myControl.Text = "" Then _
                          ErrorMessage1() Else 
    
                txtFirstName.Text = values(Convert.ToInt32 _
                (TextBoxIndices.txtFirstName))
                txtLastName.Text = values(Convert.ToInt32 _
                (TextBoxIndices.txtLastName))
                txtStudentId.Text = values(Convert.ToInt32 _
                (TextBoxIndices.txtStudentId))
    
            Next
    
        End Sub
    
       
        Private Sub btnEnter_Click(ByVal sender As System.Object, _
                                   ByVal e As System.EventArgs) _
                                   Handles btnEnter.Click
    
            Dim values As String() = GetTextBoxValues()
    
            Dim record As New GradeBookDataCollector()
    
            Me.SetTextBoxValues(GetTextBoxValues)
            If values(TextBoxIndices.txtFirstName) <> "" Then
    
                Try
                    Dim strFirstName As String = _
                        values(TextBoxIndices.txtFirstName)
    
                    If strFirstName <> "" Then
                        record.strFirstName = values(TextBoxIndices.txtFirstName)
                        record.strLastName = values(TextBoxIndices.txtLastName)
                        record.strCourseName = values(ComboBoxIndices.cboCourseName)
                        record.strStudentId = values(TextBoxIndices.txtStudentId)
                        record.strGrade = values(ComboBoxIndices.cboGrade)
    
                        fileWriter.WriteLine( _
                        record.strFirstName.ToString & "," & record.strLastName.ToString & "," & _
                        record.strStudentId.ToString & "," & record.strCourseName.ToString & "," & _
                        record.strGrade.ToString)
    
    
                        fileWriter.Flush()
    
    
                    Else
                        MessageBox.Show("Invalid First Name", "Error", _
                        MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If
    
                Catch ex As IOException
                    MessageBox.Show("Error Writing to File", "Error", _
                    MessageBoxButtons.OK, MessageBoxIcon.Error)
    
                Catch ex As FormatException
                    MessageBox.Show("Invalid Format", "Error", _
                   MessageBoxButtons.OK, MessageBoxIcon.Error)
    
                End Try
            End If
    
            Me.SetTextBoxValues(GetTextBoxValues)
            Me.ClearFormDataEntryBoxes()
    
        End Sub
        Public Sub ErrorMessage1()
    
            MsgBox("Please fill in all the fields to save the record", _
            MsgBoxStyle.Exclamation, "Record Entry Incomplete")
    
        End Sub
    
        Public Function GetTextBoxValues() As String()
    
            Dim values(TextBoxCount) As String
    
            ' copy text box fields to string Array
            values(Convert.ToInt32(TextBoxIndices.txtFirstName)) = _
                txtFirstName.Text
    
            values(Convert.ToInt32(TextBoxIndices.txtLastName)) = _
                txtLastName.Text
    
            values(Convert.ToInt32(TextBoxIndices.txtStudentId)) = _
                txtStudentId.Text
            Return values
    
        End Function
    
        Private Sub btnExit_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btnExit.Click
    
            If output IsNot Nothing Then
    
                Try
                    fileWriter.Close()
                    output.Close()
    
                Catch ex As IOException
                    MessageBox.Show("Cannot close file", " Error", _
                    MessageBoxButtons.OK, MessageBoxIcon.Error)
    
                End Try
            End If
    
            Application.Exit()
    
        End Sub
     Private Sub btnSaveAs_Click(ByVal sender As System.Object, _
                                    ByVal e As System.EventArgs) _
                                    Handles btnSaveAs.Click
    
            Dim fileChooser As New SaveFileDialog()
            Dim result As DialogResult = fileChooser.ShowDialog()
            Dim fileName As String
    
            fileChooser.CheckFileExists = False
    
            If result = Windows.Forms.DialogResult.Cancel Then
                Return
            End If
    
            fileName = fileChooser.FileName
    
            If fileName = "" Or fileName Is Nothing Then
                MessageBox.Show("Invalid File Name", "Error", _
                MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
    
                Try
                    output = New FileStream( _
                       fileName, FileMode.OpenOrCreate, FileAccess.Write)
    
                   
    fileWriterCReated Code:
    1. fileWriter = New StreamWriter(output)
    :wave: btnSaveAs.Enabled = False btnSaveAs.Enabled = True Catch ex As IOException MessageBox.Show("Error Opening File", "Error", _ MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End If End Sub End Sub End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width