• 全部
  • VBS
  • .NET技术
  • VBA
  • VB网络编程
  • VB多媒体
  • VB数据库
  • VB控件
  • VB COM/DCOM
  • VB基础类
  • VB API
  • 问答

请高手看看:索引超出数组界限问题

qsltz 2004-09-14 11:24:18
我用DATAGRID绑定数据集中的一个表A,用BINDINGmanagerbase管理绑定
只要MOUSE在DATAFRID的垂直滚动条上按一下或移动,然后新增记录时,会出现如下错误,是什么原因?

"向原始数据区提交数据时出错........"
然后点击DATAGRID出现如下:

未处理的“System.IndexOutOfRangeException”类型的异常出现在 system.windows.forms.dll 中。
索引超出数组界限.
代码如下:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim bmmaster As BindingManagerBase
Dim strdataitem As String
Dim cnwhxz As New OleDbConnection
Dim dsusers As New DataSet
Public dausers As New OleDbDataAdapter


#Region " Windows 窗体设计器生成的代码 "

#End Region
Public Sub uploadadddata()
updatadata()


End Sub
Public Sub uploadmodifydata()
updatadata()


End Sub
Public Sub uploaddeleteData()
updatadata()


End Sub
Public Sub filldata()

dausers.Fill(Dsusers)



End Sub

Private Sub updatadata()
dausers.Update(Dsusers, "users")


End Sub



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

cnwhxz.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Application.StartupPath & "\data\whxz.mdb;User Id=admin;Password=;"

Dim myselecttext As String = "SELECT * FROM users"

Dim selectcmd As OleDbCommand = New OleDbCommand(myselecttext, cnwhxz)

selectcmd.CommandTimeout = 30
dausers.SelectCommand = selectcmd
dausers.DeleteCommand = New OleDbCommand("DELETE FROM users WHERE userid=?", cnwhxz)

dausers.InsertCommand = New OleDbCommand("INSERT INTO users(userid, username) " & _
"VALUES (?, ?)", cnwhxz)


dausers.UpdateCommand = New OleDbCommand("UPDATE users SET userID = ?, username = ? " & _
"WHERE userID = ?", cnwhxz)

dausers.InsertCommand.Parameters.Add("@userid", OleDbType.Char, 8, "userid")
dausers.InsertCommand.Parameters.Add("@username", OleDbType.VarChar, 40, "username")

dausers.UpdateCommand.Parameters.Add("@userID", OleDbType.Char, 8, "userID")
dausers.UpdateCommand.Parameters.Add("@username", OleDbType.VarChar, 40, "username")
dausers.UpdateCommand.Parameters.Add("@olduserid", OleDbType.Char, 8, "userid").SourceVersion = DataRowVersion.Original

dausers.DeleteCommand.Parameters.Add("@userid", OleDbType.Char, 8, "userID").SourceVersion = DataRowVersion.Original
dsusers.EnforceConstraints = False
dausers.Fill(dsusers, "users")
dsusers.EnforceConstraints = True

' dsusers.Tables("users").PrimaryKey = New DataColumn() {dsusers.Tables("users").Columns("userid")}


' Dim pkusers As Constraint = dsusers.Tables("users").Constraints.Add( _
' "PK_userid", dsusers.Tables("users").Columns("userid"), True)

txid.DataBindings.Clear()
txname.DataBindings.Clear()


txid.DataBindings.Add(New Binding("text", dsusers, "users.userid"))
txname.DataBindings.Add(New Binding("text", dsusers, "users.username"))

DataGrid1.SetDataBinding(dsusers, "")
DataGrid1.DataMember = "users"
DataGrid1.DataSource = dsusers


bmmaster = Me.BindingContext(dsusers, "users")


End Sub
Private Sub bnfirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnfirst.Click
bmmaster.Position = 0

End Sub

Private Sub bnprior_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnprior.Click
If bmmaster.Position <> 0 Then
bmmaster.Position -= 1

End If
End Sub

Private Sub bnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnnext.Click
If bmmaster.Position <> bmmaster.Count Then
bmmaster.Position += 1
Else
bmmaster.Position = bmmaster.Count

End If
End Sub

Private Sub bnlast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnlast.Click
bmmaster.Position = bmmaster.Count

End Sub

Private Sub bnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnadd.Click
Try
bmmaster.EndCurrentEdit()
bmmaster.AddNew()

Catch err As ApplicationException
MessageBox.Show(err.ToString, "新增记录发生错误", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)

End Try

txid.Focus()

End Sub

Private Sub bncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bncancel.Click

bmmaster.CancelCurrentEdit()




End Sub

Private Sub bndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bndelete.Click
If bmmaster.Count > 0 Then
If MessageBox.Show("确定删除本记录吗?", "删除确认", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = _
DialogResult.Yes Then

bmmaster.RemoveAt(bmmaster.Position)
End If

End If
End Sub


Private Sub bnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnupdate.Click

dsusers.EnforceConstraints = False
bmmaster.EndCurrentEdit()
If dsusers.HasChanges(DataRowState.Deleted) Then
Try
uploaddeleteData()
dsusers.AcceptChanges()

Catch err As SystemException
dsusers.RejectChanges()
MessageBox.Show(Err.ToString)

End Try
End If

If dsusers.HasChanges(DataRowState.Added) Then
Try
uploadadddata()
dsusers.AcceptChanges()

Catch err As System.Data.OleDb.OleDbException
dsusers.RejectChanges()
MessageBox.Show(err.ToString)
Catch err As System.SystemException
dsusers.RejectChanges()
MessageBox.Show(err.ToString)


End Try
End If

If dsusers.HasChanges(DataRowState.Modified) Then
Try
uploadmodifydata()
dsusers.AcceptChanges()

Catch err As System.SystemException
dsusers.RejectChanges()
MessageBox.Show(err.ToString)

End Try
End If
dsusers.EnforceConstraints = True
MessageBox.Show("数据更新完成!", "消息", MessageBoxButtons.OK, _
MessageBoxIcon.Information)

End Sub


Private Sub bnload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnload.Click
dsusers.EnforceConstraints = False
filldata()
dsusers.EnforceConstraints = True
MessageBox.Show("数据填入完成!", "消息", MessageBoxButtons.OK, _
MessageBoxIcon.Information)

End Sub

...全文
74 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
lglesias 2004-09-14
up
回复
loveerror 2004-09-14
我觉得首先需要确定出错的具体位置,然后才去想为什么会出现错误.
楼主可以设置错误检测机制,查清出错的具体位置.
祝你好运.
回复
loveerror 2004-09-14
先帮你顶.看看先.
回复
dofly 2004-09-14
没遇到过这种情况,帮你顶一下了.
回复
相关推荐
发帖
VB
创建于2007-09-28

1.6w+

社区成员

VB技术相关讨论,主要为经典vb,即VB6.0
申请成为版主
帖子事件
创建了帖子
2004-09-14 11:24
社区公告
暂无公告