Reputation: 554
I have an issue with deleting the last item within my listview. If I have, let's say 3 contacts on the list view, deleting 2 out of the 3 contacts is no problem. The listview will automatically refresh and the 2 out of the 3 contacts will be removed from the list. However, if I were to delete the last and third contact, the listview will not update or refresh unless I exit from the form and re-open it.
Below is my code
Private Sub ContactTab_load()
If sConnection.State = ConnectionState.Closed Then
sConnection.ConnectionString = Search.sqlConnect
sConnection.Open()
End If
Dim sqlAdapter As New MySqlDataAdapter
Dim sqlCommand As New MySqlCommand
Dim sqlTable As New DataTable
Dim sqlText As String
nameForm.nameSearch = nameForm.nameListView.SelectedItems(0).Text
Dim InstitutionContactName As String = nameForm.nameSearch
InstitutionContactName = InstitutionContactName.Replace("'", "''")
sqlText = "select c.contact_id, c.contact_first_name,c.contact_last_name,c.primary_phone,c.primary_email, ct.contact_type_name, ihc.institution_id from institution i left outer join institution_has_contact ihc on i.institution_id = ihc.institution_id left outer join contact c on ihc.contact_id = c.contact_id join contact_type ct on ct.contact_type_id = c.contact_type_id where institution_name='" & InstitutionContactName & "' order by contact_first_name"
If Search.debugging Then
MsgBox("sql=" & sqlText)
End If
With sqlCommand
.CommandText = sqlText
.Connection = sConnection
End With
With sqlAdapter
.SelectCommand = sqlCommand
.Fill(sqlTable)
End With
If sqlTable.Rows.Count > 0 Then
InstitutionContactListView.Items.Clear()
For i = 0 To sqlTable.Rows.Count - 1
With InstitutionContactListView
.Items.Add(sqlTable.Rows(i)("contact_id"))
With .Items(.Items.Count - 1).SubItems
.Add(sqlTable.Rows(i)("contact_first_name"))
.Add(sqlTable.Rows(i)("contact_last_name"))
.Add(sqlTable.Rows(i)("primary_phone"))
.Add(sqlTable.Rows(i)("primary_email"))
.Add(sqlTable.Rows(i)("contact_type_name"))
End With
End With
Next
sqlTable.Dispose()
sqlCommand.Dispose()
sqlAdapter.Dispose()
End If
If InstitutionContactListView.Items.Count > 0 Then
InstitutionContactListView.Items(0).Selected = True
End If
End Sub
And this is the code to remove the contacts
Private Sub RemoveContactButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveContactButton.Click
If ContactIDSelected > -1 Then
RunSql("delete from institution_has_contact where institution_id = " & InstitutionID & " and contact_ID=" & ContactIDSelected)
ContactTab_load()
Else
MsgBox("Select a Contact to be removed")
End If
End Sub
Any ideas why this would happen?
Upvotes: 0
Views: 1355
Reputation: 38875
Your ContactTab_load
procedure does not account for no rows returned from the query:
If sqlTable.Rows.Count > 0 Then
InstitutionContactListView.Items.Clear()
For i = 0 To sqlTable.Rows.Count - 1
With InstitutionContactListView
' ...
Move the Clear to outside the loop, so that when you delete/remove the last item, the LV is cleared:
InstitutionContactListView.Items.Clear()
If sqlTable.Rows.Count > 0 Then
For i = 0 To sqlTable.Rows.Count - 1
With InstitutionContactListView
' ...
Alternatively, add an ELSE to do the same.
Upvotes: 1