我想知道为什么每次我到这个阶段我的程序运行这个错误?它所做的只是从数据库获取记录并将其显示在组合框上?集合被修改枚举操作可能不会在运行时执行错误
我得到两个错误:
集合被修改,枚举操作可能不会执行。在查询表达式“(用名字,姓<> '先生')
和
语法错误(逗号)。
代码下面附:
Try
'Get REGISTRATION classes
Dim RD As OleDbDataReader
Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\DATA.accdb")
Dim commStr As String = "Select Registration from students WHERE (Registration <> '"
Dim comm As OleDbCommand
con.Open()
If step4regcombobox.Items.Count = 0 Then
Dim cmd As New OleDbCommand("Select Registration from students", con)
RD = cmd.ExecuteReader()
While RD.Read
step4regcombobox.Items.Add(RD.GetString(0))
End While
End If
For Each i As Object In step4regcombobox.Items
comm = New OleDbCommand(commStr & i & "')", con)
RD = comm.ExecuteReader
While RD.Read
step4regcombobox.Items.Add(RD.GetString(0))
Exit While
End While
Next
Catch ex As Exception
MsgBox("Failure to return classes on Step 4." & vbNewLine & ex.Message)
End Try
Try
'Get TEACHERS
Dim RD2 As OleDbDataReader
Dim con2 As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\DATA.accdb")
Dim commStr2 As String = "Select Title , Forename , Surname from teachers WHERE (Forename , Surname <> '"
Dim comm2 As OleDbCommand
con2.Open()
If step4teachercombobox.Items.Count = 0 Then
Dim cmd2 As New OleDbCommand("Select Title , Forename , Surname from teachers", con2)
RD2 = cmd2.ExecuteReader()
While RD2.Read
step4teachercombobox.Items.Add(RD2.GetString(0))
End While
End If
For Each i2 As Object In step4teachercombobox.Items
comm2 = New OleDbCommand(commStr2 & i2 & "')", con2)
RD2 = comm2.ExecuteReader
While RD2.Read
step4teachercombobox.Items.Add(RD2.GetString(0))
Exit While
End While
Next
Catch ex As Exception
MsgBox("Failure to return teachers on Step 4." & vbNewLine & ex.Message)
End Try
step4.Visible = True
你没有刻意去告诉我们发生收集错误,所以我会在'For Each i2 As Object In Step4teachercombobox.Items'块中猜测它。在该块内部,您将添加到您无法执行的项目集合 - 您无法迭代集合并同时对其进行修改。没有任何需要 - 将DataTable绑定到CBO。请阅读[问]并参加[游览];每个帖子有1个问题,请提供完整的信息 – Plutonix
第二个错误应该很清楚 - 查询WHERE(Forename,Surname <>''没有任何意义。 – Filburt
您的应用程序也泄漏资源和错误消息请参见[从循环中删除集合中的项目](http://stackoverflow.com/a/20176182/1070452) – Plutonix