在写入文本文件时,从字符串“Name”到“Integer”类型的转换无效

时间:2014-08-09 00:31:50

标签: vb.net streamwriter

我花了大约一个小时来解决这个问题而无法找到解决方案。错误就行了: sw.Write(.Item(“Name”)。ToString&“,”)

Name字段是String数据类型。

If File.Exists(fileSavePath) Then
    Using sw As StreamWriter = New StreamWriter(fileSavePath, False)
        'Write Column Headers
        sw.Write("Name" & "," & "Unit Number" & "," & "Sunday" & "," & "Monday" & "," & "Tuesday" & "," & "Wednesday" & "," & "Thursday" & "," & "Friday" & "," & "Saturday")
        sw.WriteLine()

        'Write Attendance Information
        For i As Integer = 0 To ScoutDataSet.Tables("AllClasses").Rows.Count - 1
            If ScoutDataSet.Tables("AllClasses").Rows(i).Item("UnitNumber").ToString = troopNumber Then
                With ScoutDataSet.Tables("AllClasses").Rows
                    sw.Write(.Item("Name").ToString & ",")              'Write Name

                    sw.Write(.Item("UnitNumber").ToString & ",")        'Write Unit Number

                    sw.WriteLine() ' Go to next scout
                End With
            End If
        Next
    End Using
End If

1 个答案:

答案 0 :(得分:0)

您的With语句适用于ScoutDataSet.Tables("AllClasses").Rows - 即行集合,因此该集合上的.Item期望行索引。相反,您需要With ScoutDataSet.Tables("AllClasses").Rows(i)

'Write Attendance Information
For i As Integer = 0 To ScoutDataSet.Tables("AllClasses").Rows.Count - 1
    If ScoutDataSet.Tables("AllClasses").Rows(i).Item("UnitNumber").ToString = troopNumber Then
        With ScoutDataSet.Tables("AllClasses").Rows(i)
            sw.Write(.Item("Name").ToString & ",")              'Write Name
            sw.Write(.Item("UnitNumber").ToString & ",")        'Write Unit Number
            sw.WriteLine() ' Go to next scout
        End With
    End If
Next