2014-01-08 68 views
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 

回答

0

此行是可疑的,只是嘗試將其刪除:

i = i - 1 

iFor循環計數器變量。它預計會在每次迭代時遞增,以便在某個點i值將達到循環的上限,即ListView2.Items.Count - 1。由於上面的代碼遞減值爲i,它永遠不會達到上限,因此會導致無限循環。

如果你沒有被上面的解釋說服,只是嘗試這種在一個新的控制檯應用程序:

For i As Integer = 0 To 100 
    i = i - 1 
Next 
Console.WriteLine("Finish loop") 

應用程序將永遠不會打印「完成循環」,但只要你刪除i = i - 1線即可立即看到輸出。所以,如果你的循環在某些情況下莫名其妙地終止,那麼正確的問題是「爲什麼它不是無限的時候應注意什麼?必須有導致它的工作一些奇怪的行爲」,而不是相反「爲什麼這個循環是無限的?

這就是我認爲:)

+0

我應該只是刪除它?當我沒有從列表視圖中刪除項目,更新行完美工作,我懷疑它有一些與listview行爲時刪除項目 – Ron

+0

我不知道爲什麼會發生。但是從你提供的代碼中,我看不到使用'i = i-1',所以有另一個理由去除它。減法後,我無法在任何行中找到「i」變量。 – har07

+0

我嘗試刪除它,但它仍然導致無限循環。 – Ron

相關問題