Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Refresh drop down list

    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?

  • #2
    Regular Coder Freon22's Avatar
    Join Date
    May 2005
    Location
    USA
    Posts
    287
    Thanks
    3
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Richiet23 View Post
    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:
    '******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.

  • #3
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Quote Originally Posted by Richiet23 View Post
    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.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •