By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,646 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,646 IT Pros & Developers. It's quick & easy.

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

P: 3
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.DataBind()
End If
Dim MyConnection1 As New SqlConnection(ConnectionString)
MyConnection1.Open()

Dim Myreader1 As SqlDataReader
Dim MyCommand1 As New SqlCommand

MyCommand1.CommandText = "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.Connection = MyConnection1
Myreader1 = MyCommand1.ExecuteReader

Dim StudentList As New StudentList
Dim Course As New Course

While Myreader1.Read

If Myreader1.HasRows And StudentList.StudentName = ddlStudentID.SelectedValue Then
Me.ListBox2.Items.Add(New ListItem(Course.CourseName))
End If
End While

'Release MyReader1
Myreader1.Close()
Myreader1 = Nothing
MyConnection1.Close()
MyConnection1.Dispose()
MyConnection1 = Nothing
MyCommand1.Dispose()
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.
Nov 25 '07 #1
Share this Question
Share on Google+
1 Reply


lotus18
100+
P: 866
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.


Expand|Select|Wrap|Line Numbers
  1. 'Fill the List box with the appropriate class for the selected student'
  2.         If IsPostBack Then
  3.             ListBox2.DataBind()
  4.         End If
  5.         Dim MyConnection1 As New SqlConnection(ConnectionString)
  6.         MyConnection1.Open()
  7.  
  8.         Dim Myreader1 As SqlDataReader
  9.         Dim MyCommand1 As New SqlCommand
  10.  
  11.         MyCommand1.CommandText = "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]"
  12.  
  13.         MyCommand1.Connection = MyConnection1
  14.         Myreader1 = MyCommand1.ExecuteReader
  15.  
  16.         Dim StudentList As New StudentList
  17.         Dim Course As New Course
  18.  
  19.         While Myreader1.Read
  20.  
  21.             If Myreader1.HasRows And StudentList.StudentName = ddlStudentID.SelectedValue Then
  22.                 Me.ListBox2.Items.Add(New ListItem(Course.CourseName))
  23.             End If
  24.         End While
  25.  
  26.         'Release MyReader1
  27.         Myreader1.Close()
  28.         Myreader1 = Nothing
  29.         MyConnection1.Close()
  30.         MyConnection1.Dispose()
  31.         MyConnection1 = Nothing
  32.         MyCommand1.Dispose()
  33.         MyCommand1 = Nothing
  34.  
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.
Nov 26 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.