1
嗨再次,我是能夠成功地刪除了我的列表框還未被退房項目更新剩餘項目時會導致無限循環,但是當我使用更新我的數據庫中的剩餘項目在列表視圖中產生無限循環。問題是在我更新剩下的列表視圖的行中,它只更新第一行,不在下一行循環。項目在列表視圖成功刪除,但在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
我應該只是刪除它?當我沒有從列表視圖中刪除項目,更新行完美工作,我懷疑它有一些與listview行爲時刪除項目 – Ron
我不知道爲什麼會發生。但是從你提供的代碼中,我看不到使用'i = i-1',所以有另一個理由去除它。減法後,我無法在任何行中找到「i」變量。 – har07
我嘗試刪除它,但它仍然導致無限循環。 – Ron