index超出了数组的范围

时间:2013-09-30 05:40:19

标签: mysql asp.net vb.net

我的try / catch表达式总是给我一个错误消息,“索引超出了数组的范围”但是该函数的工作方式完全正常。唯一的问题是烦人的错误。

这是我正在使用的代码:

        Try
        conn.Open()
        Dim Qwery As String = "SELECT a.accid AS 'No', a.setID AS 'ID', l.class AS 'Type', CONCAT(fname,' ',middname,' ',lname ) AS 'Name', date(a.bdate) AS 'BirthDate', l.mail AS 'Acount Name', l.login AS 'Login',  l.gender AS 'Gender', l.position AS 'Position', l.department AS 'Department',  l.tel AS 'Tel No', l.localno AS 'Office Tel', l.cellno AS 'Cell Phone', l.alt_email AS 'Other Email', l.classif AS 'Classification', l.status AS 'Status' FROM entry a, liist l WHERE a.accid = l.accid AND l.status = '0' ORDER BY a.accid Desc"

        Dim smd As MySqlCommand
        Dim myreader As MySqlDataReader
        smd = New MySqlCommand(Qwery, myconn)
        myreader = smd.ExecuteReader
        If myreader.Read() = True Then
            IdBox.Text = myreader.GetValue(0)
            IdNumBox.Text = myreader.GetValue(1)
            TypeBox.Text = myreader.GetValue(2)
            NameBox.Text = myreader.GetValue(3)
            BDayBox.Text = myreader.GetValue(4)
            AcNameBox.Text = myreader.GetValue(5)
            PassBox.Text = myreader.GetValue(6)
            GenBox.Text = myreader.GetValue(7)
            PositionBox.Text = myreader.GetValue(8)
            DepartmentBox.Text = myreader.GetValue(9)
            TelBox.Text = myreader.GetValue(10)
            OfficeBox.Text = myreader.GetValue(11)
            CellBox.Text = myreader.GetValue(12)
            altMailBox.Text = myreader.GetValue(13)
            ClassBox.Text = myreader.GetValue(14)
            StatusBox.Text = myreader.GetValue(15)
        End If

        conn.Close()
    Catch ex As Exception
        MsgBox("Error: " & ex.Message.ToString())
        conn.Close()
    End Try

有趣的是,我尝试逐个删除标签的分配。并且最多myreader.GetValue(3)错误不会出现。如果我尝试分配更多标签,它会再次出现。

有谁知道背后的原因?

2 个答案:

答案 0 :(得分:0)

检查datareader的条件是否超过15行。然后访问您的代码。

myreader = smd.ExecuteReader
        If myreader.Read() = True Then
if myreader.GetSchemaTable().Rows.Count >= 15 then
            IdBox.Text = myreader.GetValue(0)
            IdNumBox.Text = myreader.GetValue(1)
            TypeBox.Text = myreader.GetValue(2)
            NameBox.Text = myreader.GetValue(3)
            BDayBox.Text = myreader.GetValue(4)
            AcNameBox.Text = myreader.GetValue(5)
            PassBox.Text = myreader.GetValue(6)
            GenBox.Text = myreader.GetValue(7)
            PositionBox.Text = myreader.GetValue(8)
            DepartmentBox.Text = myreader.GetValue(9)
            TelBox.Text = myreader.GetValue(10)
            OfficeBox.Text = myreader.GetValue(11)
            CellBox.Text = myreader.GetValue(12)
            altMailBox.Text = myreader.GetValue(13)
            ClassBox.Text = myreader.GetValue(14)
            StatusBox.Text = myreader.GetValue(15)
        End If
 End If

或尝试datareaderObject.RecordsAffected >= 15

答案 1 :(得分:0)

我认为在你的查询日期(a.bdate)AS'LirthDate'正在抛出错误

可能是因为a.bdate为空值而无法转换为日期..