Results 1 to 9 of 9

Thread: Filling a drop down list from db at IndexChanged

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Filling a drop down list from db at IndexChanged

    I am attempting to fill a drop down list (from a database) once I choose a value from another drop down list. For instance the first drop down list is named ddlYear. When I select the year I want the next list to fill with all the makes from that year. Here is the code I have so far.
    VB Code:
    1. Protected Sub ddlYear_SelectedIndexChanged( _
    2.         ByVal sender As Object, _
    3.         ByVal e As System.EventArgs) Handles ddlYear.SelectedIndexChanged
    4.  
    5.         If IsPostBack = True Then
    6.             Dim cnn As New OleDbConnection(obConn)
    7.             Dim cmd As New OleDbCommand
    8.             cmd = cnn.CreateCommand
    9.             cmd.Connection = cnn
    10.             Dim da As New OleDbDataAdapter
    11.             Dim dsMake As New DataSet
    12.             Try
    13.                 cnn.Open()
    14.                 cmd.CommandText = "Select Make From tblRecall Where Year = '" & _
    15.                     ddlYear.Text & "'"
    16.                 da.SelectCommand = cmd
    17.                 da.Fill(dsMake, "Makes")
    18.                 ddlMake.DataSource = dsMake.Tables("Makes")
    19.                 ddlMake.DataMember = ("Makes")
    20.             Catch ex As Exception
    21.                 WriteLine(ex.Message)
    22.             Finally
    23.                 cnn.Close()
    24.                 da.Dispose()
    25.             End Try
    26.         End If
    27.     End Sub
    After I select the year nothing at all happens. What am I doing wrong?
    Last edited by FastEddie; Aug 20th, 2006 at 09:55 PM.

  2. #2
    Addicted Member silentthread's Avatar
    Join Date
    Jun 2006
    Location
    Miami, Florida
    Posts
    143

    Re: Filling a drop down list from db at IndexChanged

    First make sure if the second dropdown list is filled even if you don't make a selection in the first one (attempt to try to isolate the problem). Try filling up both dropdown lists on the page load.

    What happens then?

    Also make sure that when you fill in the year dropdown list (in this case the first one) that it is filled up only once. Something like...

    VB Code:
    1. If Not Page.IsPostBack Then
    2. ' fill up year dropdown list
    3. End if
    Watch media as you download it! Excellent tool!
    FREE CUBA!
    MyBlog
    If you feel my post has helped, please rate it.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Filling a drop down list from db at IndexChanged

    silentthread I tried isolating it by putting the code I posted behind a button click and hard coding the year to 2000 and neither made any difference. I am not getting any errors. It is just not loading the drop down list.

  4. #4
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: Filling a drop down list from db at IndexChanged

    FE you are setting the datasource for the dropdownlist but not databinding it. All you need to add is bind your control to that data.
    Code:
                    ddlMake.DataSource = dsMake.Tables("Makes")
                    ddlMake.DataMember = ("Makes")
                    ddlMake.DataBind()

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Filling a drop down list from db at IndexChanged

    This is what I have and I am still not getting the drop down lis filled. No error message and I am doing this with a button (for testing purposes). The page does repost but the list stays empty.
    VB Code:
    1. Dim cnn As New OleDbConnection(obConn)
    2.         Dim cmd As New OleDbCommand
    3.         cmd = cnn.CreateCommand
    4.         cmd.Connection = cnn
    5.         Dim da As New OleDbDataAdapter
    6.         Dim dsMake As New DataSet
    7.         Try
    8.             cnn.Open()
    9.             cmd.CommandText = "Select Distinct Make From tblRecall Where Year = 2000"
    10.             da.SelectCommand = cmd
    11.             da.Fill(dsMake, "Makes")
    12.             ddlMake.DataSource = dsMake.Tables("Makes")
    13.             ddlMake.DataMember = ("Makes")
    14.             ddlMake.DataBind()
    15.         Catch ex As Exception
    16.             'WriteLine(ex.Message)
    17.         Finally
    18.             cnn.Close()
    19.             da.Dispose()
    20.         End Try

    The drop down looks like this:
    VB Code:
    1. <td style="width: 100px">
    2.     <asp:DropDownList ID="ddlMake" runat="server" />
    3. </td>

  6. #6
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: Filling a drop down list from db at IndexChanged

    ah ok. I don't usually do it quite the same way as you as I tend to use DataReaders instead of DataAdapters.

    So anyway, if DataMember does what I think it does you still aren't assigning text and data values to your dropdownlist. Here how I do it.
    Code:
            this.ddlSector.DataSource = flaxwebnet.BusinessLogic.Sector.Combo();
            this.ddlSector.DataTextField = "Value";
            this.ddlSector.DataValueField = "ID";
            this.ddlSector.DataBind();

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Filling a drop down list from db at IndexChanged

    Thanks Fishcake your sample helped me get it right. I had two problems. The first being that I was using distinct in my SQL incorrectly and the next is that I was using the year as a number instead of text. Once I took that Try Catch out of the code it was made clear to me.

    Thanks for giving me the code sample though because without it I wouldn't have continued to tweak this until I got it figured out.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Re: Filling a drop down list from db at IndexChanged

    One more question. I am now getting my dropdown lists to fill when I press the buttons but I want to make it fill when I change the value in the preceeding drop down list.
    VB Code:
    1. Protected Sub ddlYear_TextChanged( _
    2.         ByVal sender As Object, _
    3.         ByVal e As System.EventArgs) _
    4.         Handles ddlYear.TextChanged
    5.  
    6.         FillMake()
    7.  
    8.     End Sub
    9.     Private Sub FillMake()
    10.         Dim cnn As New OleDbConnection(obConn)
    11.       .....
    12.     End Sub
    Do I have to do something else to fire the event.

  9. #9
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: Filling a drop down list from db at IndexChanged

    Set 'Autopostback=true' on your first dropdownlist and then place your code in the dropdownlists SelectedIndexChanged event.

Posting Permissions

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



Click Here to Expand Forum to Full Width