特定数据库列上的System.InvalidCastException错误

时间:2017-03-09 17:54:02

标签: vb.net oledb

所以我目前正在尝试从访问数据库读取值并将它们写入VB.NET中的标签数组。这是我正在制作的一个小测验程序,他们在14个类别中获得的先前分数被写入屏幕。我遇到的问题是当写入Label7(数据库中的第20列)时,我收到错误'指定的强制转换无效'。我已经彻底检查了我的txt文件和数据库,看看这个特定值有什么问题,我看不出有什么区别。它仍然被写为32位整数,就像所有其他整数一样,代码没有问题。

Dim LabelArray() As Control = {Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8, Label9, Label10, Label11, Label12, Label13, Label14}
    Dim x As Integer = 2
'starting at the the 3rd column in the database

    Dim QArray(13) As String 'reading names of the columns from a txt file into the array
    FileOpen(1, Application.StartupPath & "\sqlscores.txt", OpenMode.Input) 'load files
    Dim j As Integer = 0
    Do While Not EOF(1)
        QArray(j) = LineInput(1) 'input into array
        j = j + 1
    Loop
    FileClose(1)

    Dim tbl As New DataTable
    Dim conn1 = MyConnection2() 'connects to microsoft access database

    Dim temp As String

    For i = 0 To 13 'for each item in the array
        Dim command = New OleDbCommand("Select * From Users Where Username='" & usernames & "' and " & QArray(i) & " >= 0", conn) 'look for that column in the database where their username and score is more than 0

        Dim reader = command.ExecuteReader()
        While reader.Read()

                temp = (reader.GetInt32(x)).ToString 'read value and convert to string

            LabelArray(i).Text = temp 'assign temp to label text

            x = x + 3 'increment by 3 as correct column is in every third column
        End While
    Next i

下面是导致问题的数据库(特定列)的屏幕截图。 The first field is the section that is playing up

As you can see, it has the same properties as a section that does work compared to a section that does

我已尝试删除该列并再次添加,这没有任何区别。我还在VB中检查了标签(并重新添加)以查看是否存在问题,但事实并非如此。它似乎认为该列中的值(当前是32位整数“4”)无法转换为字符串。我只对db中的练习分数感兴趣,可以在列名中看到。

我错过了一些明显的东西吗?

0 个答案:

没有答案