How do I fill my listbox from a selection from the drop down list.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Boheim
    New Member
    • Nov 2007
    • 3

    How do I fill my listbox from a selection from the drop down list.

    I have two controls on my page:
    1) A list box
    2)A drop down list

    I want my users to be able to select from the drop down list and the related data be shown in the list box.
    For example: lets say the data in the combo box were the names of various students. I want the users to select a student name and then their classes be listed in the list box.



    Ok. This is my code so far.


    Code: ( vb )
    'Fill the List box with the appropriate class for the selected student'
    If IsPostBack Then
    ListBox2.DataBi nd()
    End If
    Dim MyConnection1 As New SqlConnection(C onnectionString )
    MyConnection1.O pen()

    Dim Myreader1 As SqlDataReader
    Dim MyCommand1 As New SqlCommand

    MyCommand1.Comm andText = "SELECT [Student List].[Student ID], [Student List].[Student Name], Course.[Course Name] FROM Course INNER JOIN [Class List] ON Course.[Course ID] = [Class List].[Course ID] INNER JOIN [Student List] ON [Class List].[Course ID] + [Class List].[Section ID] + [Class List].[Semester ID] + [Class List].[Class Year] = [Student List].[Student Class]"

    MyCommand1.Conn ection = MyConnection1
    Myreader1 = MyCommand1.Exec uteReader

    Dim StudentList As New StudentList
    Dim Course As New Course

    While Myreader1.Read

    If Myreader1.HasRo ws And StudentList.Stu dentName = ddlStudentID.Se lectedValue Then
    Me.ListBox2.Ite ms.Add(New ListItem(Course .CourseName))
    End If
    End While

    'Release MyReader1
    Myreader1.Close ()
    Myreader1 = Nothing
    MyConnection1.C lose()
    MyConnection1.D ispose()
    MyConnection1 = Nothing
    MyCommand1.Disp ose()
    MyCommand1 = Nothing

    The StudentList and the Course are two tables that I created in my database.
    When I execute this code there is nothing shown in Listbox2. I only want the specific classes for the selected student shown in listbox2. Please help and thanks in advance.
  • lotus18
    Contributor
    • Nov 2007
    • 865

    #2
    Originally posted by Boheim
    I have two controls on my page:
    1) A list box
    2)A drop down list

    I want my users to be able to select from the drop down list and the related data be shown in the list box.
    For example: lets say the data in the combo box were the names of various students. I want the users to select a student name and then their classes be listed in the list box.



    Ok. This is my code so far.


    [code=vb]
    'Fill the List box with the appropriate class for the selected student'
    If IsPostBack Then
    ListBox2.DataBi nd()
    End If
    Dim MyConnection1 As New SqlConnection(C onnectionString )
    MyConnection1.O pen()

    Dim Myreader1 As SqlDataReader
    Dim MyCommand1 As New SqlCommand

    MyCommand1.Comm andText = "SELECT [Student List].[Student ID], [Student List].[Student Name], Course.[Course Name] FROM Course INNER JOIN [Class List] ON Course.[Course ID] = [Class List].[Course ID] INNER JOIN [Student List] ON [Class List].[Course ID] + [Class List].[Section ID] + [Class List].[Semester ID] + [Class List].[Class Year] = [Student List].[Student Class]"

    MyCommand1.Conn ection = MyConnection1
    Myreader1 = MyCommand1.Exec uteReader

    Dim StudentList As New StudentList
    Dim Course As New Course

    While Myreader1.Read

    If Myreader1.HasRo ws And StudentList.Stu dentName = ddlStudentID.Se lectedValue Then
    Me.ListBox2.Ite ms.Add(New ListItem(Course .CourseName))
    End If
    End While

    'Release MyReader1
    Myreader1.Close ()
    Myreader1 = Nothing
    MyConnection1.C lose()
    MyConnection1.D ispose()
    MyConnection1 = Nothing
    MyCommand1.Disp ose()
    MyCommand1 = Nothing
    [/code]
    The StudentList and the Course are two tables that I created in my database.
    When I execute this code there is nothing shown in Listbox2. I only want the specific classes for the selected student shown in listbox2. Please help and thanks in advance.
    Hi Boheim

    I'm not good in vb .net I think with your sql statement you need to add a condition (where) .

    For example: lets say the data in the combo box were the names of various students. I want the users to select a student name and then their classes be listed in the list box.
    I think it would be better to select with their id number rather than their names when using a combobox.

    Comment

    Working...