...

View Full Version : Refresh drop down list



Richiet23
02-21-2007, 12:37 PM
I have a drop down list connected to an access database. When a command button is pressed, the selected item in the drop down list is deleted. the item that is deleted stays in the drop down list though, can anyone give me any help on how to delete the item from the list box as it is deleted from the database please?

Freon22
02-21-2007, 02:18 PM
I have a drop down list connected to an access database. When a command button is pressed, the selected item in the drop down list is deleted. the item that is deleted stays in the drop down list though, can anyone give me any help on how to delete the item from the list box as it is deleted from the database please?

You really need to show some code in order to get help. But I will try to point you in the right direction.

If you are deleteing an item that is in your dropdown list from the database then you need to reload the dropdown list with the new data. Here is an example. So everytime you delete an item from your database you need to call a sub to clear your dropdown list and to refill it with the new values.


'******Code for filling the dropdown list.******
Private Sub FillDropdownList()
myDropdownList.Items.Clear()

'Select item_name and item_id from a table
Dim SelectSql As String = "SELECT item_name, item_id FROM myTable"

'Define the ADO.NET objects.
Dim con As New SqlConnection(connectionString)
Dim cmd As New SqlCommand(SelectSql, con)
Dim reader As SqlDataReader

'Try to open database and read information.
Try
con.Open()
reader = cmd.ExecuteReader()

'For each item, add the item_name to the display list box text,
'and store the unique ID in the Value property.
Do While reader.Read()
Dim newItem As New ListItem()
newItem.Text = reader("item_name")
newItem.Value = reader("item_id").ToString
myDropdownList.Items.Add(newItem)
Loop
myDropdownList.Items.Insert(0, New ListItem("---Choose A Item To Delete---", ""))
reader.Close()

Catch err As Exception
lblResults2.Text = "Error in reading list of database table. "
lblResults2.Text &= err.Message
Finally
con.Close()
End Try
End Sub


Hope this help because I don't know what method you are using to fill your dropdown list. But whatever method you are using you will have to clear the dropdown list a repopulate it.

nikkiH
02-21-2007, 02:53 PM
I have a drop down list connected to an access database. When a command button is pressed, the selected item in the drop down list is deleted. the item that is deleted stays in the drop down list though, can anyone give me any help on how to delete the item from the list box as it is deleted from the database please?

You need to refresh the datasource and databind again. So whatever code you used to populate it to begin with, call that again. :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum