R
Randy
I have button that deletes items from a SQL datatable. The items are
displayed for the user in listbox, which is bound to the dataset. The
code works just fine insofar as it deletes the correct record from the
dataset and the datasource. What I need to make it do is to refresh
the listbox so that it reflects that the record has been deleted.
Right now, the listbox removes the first item from the list and leaves
the deleted item displayed. If I close the form and reopen it, all
appears as it should (e.g. the correct item is gone and the listbox
displays correctly). How can I force the listbox to refresh as if the
form were re-loading.?
Here is the code:
Dim strSQL As String
strSQL = "DELETE Category WHERE Category = @Cat"
Dim cmdDelete As New SqlCommand(strSQL, cn)
cmdDelete.Parameters.AddWithValue("@Cat",
lbCat.SelectedValue)
Try
cn.Open()
Dim intRecordsAffected As Integer
intRecordsAffected = cmdDelete.ExecuteNonQuery()
If intRecordsAffected = 1 Then
CategoryBindingSource.RemoveCurrent()
CategoryBindingSource.MoveFirst()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cn.Close()
End Try
Thanks a lot,
Randy
displayed for the user in listbox, which is bound to the dataset. The
code works just fine insofar as it deletes the correct record from the
dataset and the datasource. What I need to make it do is to refresh
the listbox so that it reflects that the record has been deleted.
Right now, the listbox removes the first item from the list and leaves
the deleted item displayed. If I close the form and reopen it, all
appears as it should (e.g. the correct item is gone and the listbox
displays correctly). How can I force the listbox to refresh as if the
form were re-loading.?
Here is the code:
Dim strSQL As String
strSQL = "DELETE Category WHERE Category = @Cat"
Dim cmdDelete As New SqlCommand(strSQL, cn)
cmdDelete.Parameters.AddWithValue("@Cat",
lbCat.SelectedValue)
Try
cn.Open()
Dim intRecordsAffected As Integer
intRecordsAffected = cmdDelete.ExecuteNonQuery()
If intRecordsAffected = 1 Then
CategoryBindingSource.RemoveCurrent()
CategoryBindingSource.MoveFirst()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cn.Close()
End Try
Thanks a lot,
Randy