您好我能够成功删除我的列表框中尚未签出的项目,但是当我使用listview中的其余项目更新我的数据库时,它会产生无限循环。问题出在我更新列表视图中剩余部分的行,它只更新第一行而不在下一行循环。
Dim ListOfExistingItem As List(Of Integer) = New List(Of Integer)
Try
For x = 0 To ListView2.Items.Count - 1
Dim a As String = ListView2.Items.Item(x).Text
a = ListView2.Items.Item(x).Text
'-------------------------Max TRansaction----------------------------------
Dim SQLMAX As String = ("Select Max(transaction_no) from tbl_list1 where barcode = '" + a + "' and [Check-In] IS NULL and [User] = '" + rsuser + "'")
Dim STRMAX As String = ""
Dim cmdmax As New SqlCommand(SQLMAX, locconn)
Dim MAXREAD As SqlDataReader
locconn.Open()
MAXREAD = cmdmax.ExecuteReader
MAXREAD.Read()
If IsDBNull(MAXREAD(0)) = False Then
STRMAX = MAXREAD(0)
End If
MAXREAD.Close()
locconn.Close()
'-----Remove items to listview that are not yet checked out-------------------------------------
Dim sqlvalreader As SqlDataReader
Dim TMP_SQL_VAL2 = "select [Check-In] from tbl_list1 where barcode = '" + a + "' and transaction_no = '" & STRMAX & "'"
Dim cmdval = New SqlCommand(TMP_SQL_VAL2, locconn)
locconn.Open()
sqlvalreader = cmdval.ExecuteReader
sqlvalreader.Read()
If sqlvalreader.HasRows Then
If IsDBNull(sqlvalreader(0)) = False Then
MsgBox("Barcode: " & a & "is still Inside", MsgBoxStyle.Exclamation)
clear_text()
ListOfExistingItem.Add(x)
End If
Else
ListOfExistingItem.Add(x)
End If
sqlvalreader.Close()
locconn.Close()
Next
Call ListOfExistingItem.Reverse()
If Not IsNothing(ListOfExistingItem) Then
For Each x As Integer In ListOfExistingItem
If Not x > ListView2.Items.Count - 1 Then
ListView2.Items.RemoveAt(x)
End If
Next
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
'------------------------------------------------------------------------------------------
'-------------------------update the remaining items in the listview-----------------------
Try
Dim i As Integer
Dim a, b, c, d As String
For i = 0 To ListView2.Items.Count - 1
a = ListView2.Items.Item(i).Text
b = ListView2.Items.Item(i).SubItems.Item(1).Text
c = ListView2.Items.Item(i).SubItems.Item(2).Text
d = ListView2.Items.Item(i).SubItems.Item(3).Text
i = i - 1
Dim SQLMAX As String = ("Select Max(transaction_no) from tbl_list1 where barcode = '" + a + "' and [Check-In] IS NULL and [User] = '" + rsuser + "'")
Dim STRMAX As String = ""
Dim cmdmax As New SqlCommand(SQLMAX, locconn)
Dim MAXREADER As SqlDataReader
locconn.Open()
MAXREADER = cmdmax.ExecuteReader
MAXREADER.Read()
If IsDBNull(MAXREADER(0)) = False Then
STRMAX = MAXREADER(0)
End If
locconn.Close()
Dim command As New SqlCommand
TMP_SQL = "update tbl_list1 " & _
"set [Check-In] = GETDATE() " & _
"where ID_ = (select top (1) ID_ " & _
"from tbl_list1 " & _
"where barcode = '" + a + "' and transaction_no = '" + STRMAX + "' and [Check-In] IS NULL) "
locconn.Open()
command = New SqlCommand(TMP_SQL, locconn)
command.ExecuteNonQuery()
locconn.Close()
Next
答案 0 :(得分:0)
此行可疑,只是尝试将其删除:
i = i - 1
i
是For
循环中的计数器变量。它期望在每次迭代时递增,以便在某个点i
值将达到循环的上限,即ListView2.Items.Count - 1
。由于上面的代码递减了i
的值,它永远不会达到上限,从而导致无限循环。
如果您不相信上述说明,只需在新的控制台应用程序中尝试:
For i As Integer = 0 To 100
i = i - 1
Next
Console.WriteLine("Finish loop")
应用程序永远不会打印"完成循环",但只要删除i = i - 1
行,您就可以立即看到输出。因此,如果你的循环在某种情况下以某种方式被终止,那么正确的问题是" 为什么它应该是无限的?必须有一些奇怪的行为导致它起作用"而不是相反的" 为什么这个循环是无限的?"
这就是我的想法:)。