我试图从一个使用VB Net和Access数据库的查询中获得一个值,并且只对第一条记录工作,然后我得到了这个错误:
在MYPROJECTPATH\Form2.vb中的System.data.rbtree‘1.1.GetNodeByIndex(Int32 userIndex) System.Data.DataRowCollection.get:Item(Int32索引)NAMEOFMYPROJECT.Form2.Button24_Click(对象发送方,EventArgs e)
这是我的片段:
For i As Integer = 0 To DataGridView2.Rows.Count - 2
Try
ConexionBD.Open()
ComandoBD.Connection = ConexionBD
ComandoBD.CommandText = "INSERT INTO Ventas (IdProducto, Fecha, Hora) VALUES (@IdProducto, @Fecha, @Hora)"
ComandoBD.Parameters.Add("@IdProducto", OleDbType.VarChar).Value = Form2.DataGridView2.Rows(i).Cells(0).Value
ComandoBD.Parameters.Add("@Fecha", OleDbType.DBDate).Value = Now
ComandoBD.Parameters.Add("@Hora", OleDbType.DBTime).Value = Now.TimeOfDay
ComandoBD.ExecuteNonQuery()
consulta = "SELECT CantidadDisponible FROM Productos WHERE Detalle='" & DataGridView2.Rows(i).Cells(1).Value.ToString & "'"
adaptador = New OleDbDataAdapter(consulta, ConexionBD)
registros = New DataSet
adaptador.Fill(registros, "Productos")
If registros.Tables("Productos").Rows.Count() <> 0 Then
msgbox(registros.Tables("Productos").Rows(i).Item(0)) 'HERE IS WHERE THE ERROR IS POINTING AT
Elseif
msgbox("There is no available......") 'THIS MESSAGE ITS NEVER SHOWN
End If
ComandoBD.Dispose()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
ConexionBD.Close()
Next
对不起,如果我的问题太基本,但我找不到为什么会发生PS:数据库不是空的,列有正确的编号。
发布于 2016-06-10 19:13:39
我发现了我的错误,我改变了这个:
msgbox(registros.Tables("Productos").Rows(i).Item(0))
为此:
msgbox(registros.Tables("Productos").Rows(0).Item(0))
愚蠢的我:P
谢谢你们所有人。
https://stackoverflow.com/questions/37753531
复制相似问题