我收到此错误FileName.exe中发生了类型为'System.IndexOutOfRangeException'的未处理的异常-它是由写入行函数引起的

问题描述

Dim outputFile As IO.Path
Dim filepath As String = "PCPT2Database.txt"
'Append a new Entry into PCPT2Database
Private Sub btnSave_Click(sender As Object,e As EventArgs) Handles btnSave.Click
    'Declare variables
    Dim newNo As Integer = 0
    Dim NewEmail As String = 0
    Dim NewSurname As String = 0
    Dim NewName As String = 0
    Dim NewCell As Integer = 0
    'Defencive Programming

    If txtName.Text = "" Or IsNumeric(txtName.Text) Then
        MessageBox.Show("Please enter a Valid Name")
        txtName.Focus()
    Else : NewName = txtName.Text
    End If
    If txtSurname.Text = "" Or IsNumeric(txtSurname.Text) Then
        MessageBox.Show("Please enter a Valid Surname")
        txtSurname.Focus()

    Else : NewSurname = txtSurname.Text
    End If
    If txtEmail.Text = "" Or IsNumeric(txtEmail.Text) Then
        MessageBox.Show("Please enter a Valid Email")
        txtEmail.Focus()
    Else : NewEmail = txtEmail.Text
    End If
    If mtbCell.Text = "" Then
        MessageBox.Show("Please enter a 10 digit number")
        mtbCell.Focus()
    End If

    Dim filePath As String = "T2Database.txt"
    Dim intNewNo() As Integer = {}
    Dim intCount As Integer = 0
    Dim currentRow() As String

    Dim intLoop As Integer

    Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(filePath)
        MyReader.TextFieldType = FileIO.FieldType.Delimited
        MyReader.SetDelimiters(",")

        While Not MyReader.EndOfData
            Try
                currentRow = MyReader.ReadFields()

                ReDim Preserve intNewNo(intCount)

                intNewNo(intCount) = currentRow(0)

                intCount = +1

            Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                MsgBox("line" & ex.Message & " Its not valid.")

            End Try
        End While
    End Using

    For intLoop = 0 To intCount + 1

    Next
    If IO.File.Exists(filePath) Then

        Using outputFile = IO.File.AppendText(filePath)
            outputFile.WriteLine**(Join({intNewNo(intLoop).ToString,txtName.Text,txtSurname.Text,mtbCell.Text,txtEmail.Text},","))**
        End Using

        MessageBox.Show("Save Successful")
        MessageBox.Show("Add another record","Cancel",MessageBoxButtons.OKCancel,MessageBoxIcon.Information)

        txtName.Clear()
        txtSurname.Clear()
        mtbCell.Clear()
        txtEmail.Clear()
    Else
        MessageBox.Show(filePath,".txt not found.",MessageBoxButtons.RetryCancel,MessageBoxIcon.Information)
    End If
End Sub

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)