473,387 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
1 1326
lotus18
866 512MB
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

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

Similar topics

4
by: John Guarnieri | last post by:
Hi All, I need some code to drag items in a list box either up or down along with not just the text but with the itemdata too. Can anyone hook me up? TIA John
8
by: Vipin Kedia | last post by:
Hi I have written a code for showing the list boxes as selected using a Listitem and the selected property of the items. Now I have 2 list boxes in my page. But it shows only the selected values...
2
by: Matt Sawyer | last post by:
Hi, I'm attempting to do a drag and drop operation from one listbox to another. I have my listboxes setup with SelectionMode = MultiExtended so that I can use the shift key, cntrl key, etc. to...
0
by: AK | last post by:
Hello, Listbox mulitselect property is set to true (extended). I am trying to implement drag and drop feature for a listbox. The drag & drop is done in the mouse move event (DoDragdrop...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
0
by: John | last post by:
I have a listbox and the values get selected (highlighted) based off of a selection from a drop down. I can highlight the corresponding list box items, but they do not show in the listbox I have...
4
by: Jeff | last post by:
....still new at this. ...hopefully a simple question Is there some practical way of altering the function of a keypress in Visual Web.net 2005 using VB without causing a postback on each...
2
by: Boheim | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.