指数超出范围。必须是非负数且小于集合的大小。参数名称:index

时间:2015-01-31 05:16:37

标签: vb.net

请帮忙!我想要更新数据,但我收到错误消息框"索引超出范围。必须是非负数且小于集合的大小。参数名称:index"。数据库Ms.Access 2010。 这是我的代码:

更新

    Try

        Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
        Dim breader As New BinaryReader(fsreader)
        Dim imgbuffer(fsreader.Length) As Byte
        breader.Read(imgbuffer, 0, fsreader.Length)
        fsreader.Close()
        With cmd
            .Connection = con
            .CommandText = "UPDATE Tbl_Admin SET Name = @nama, Pwd_Admin = @pass, Information = @info, namafile = @filename, picture = @gambar WHERE ID = " & dg1.SelectedRows(0).Cells(0).Value & ""
            .Parameters.AddWithValue("@nama", TxtNamaAdmin.Text)
            .Parameters.AddWithValue("@pass", TxtPassAdmin.Text)
            .Parameters.AddWithValue("@info", TxtStatusAdmin.Text)
            .Parameters.AddWithValue("@filename", LbNamePic.Text)
            .Parameters.AddWithValue("@gambar", imgbuffer)
            .ExecuteNonQuery()
            .Dispose()
        End With

        tampildata()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

2 个答案:

答案 0 :(得分:2)

以下行可能导致错误:

.CommandText = "UPDATE Tbl_Admin SET Name = @nama, Pwd_Admin = @pass,
Information = @info, namafile = @filename, picture = @gambar WHERE ID = " & 
dg1.SelectedRows(0).Cells(0).Value & ""

在运行命令之前检查dg1.SelectedRows(0).Cells(0).Value的值。很可能这个约束可能没有任何价值。

答案 1 :(得分:-1)

尝试将您的值转换为整数,如下所示

var intdg1selected As Integer =  Convert.ToInt32(dg1.SelectedRows(0).Cells(0).Value.ToString())

如果您已创建自动连接,请检查您的连接名称是否正确。