If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Combo Box Help

Hey everybody

I am creating one application in which I am stuck at one point. I want to relate two combo boxes for example
let the first combo box be named "bxcourse"
n the second one is "bxlevel"

for bxcourse in want to select and list DISTINCT values from the database and based on the value selected from bxcourse in want to search from the database and then add the respective items in the bxlevel.

I m able to select and display the distinct values in bxcourse but I m not able to display the values in bxlevel based on the selected item from bxcourse.
Please help me out!!

React to the SelectedIndexChanged event of the courses ComboBox, which is triggered each time the user makes a selection.

When this event is triggered, read the selected value and send it to the procedure that retrieves the data and bind it or fills the levels in their ComboBox.

Something such as the following:

Code:

Private Sub cboCourse_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCourse.SelectedIndexChanged
FillLevel(cboCourse.Text) 'or maybe cboCourse.SelectedValue if you defined a ValueMember when you initialized cboCourse
End Sub
Private Sub FillLevel(ByVal course As String)
'Retrieve level values for the course
'Bind the result to bxlevel or fill cbolevel, depending on your method of choice
End Sub

Note that I have taken the liberty of changing the names of the controls, from bxcourse to cboCourse.

cboCourse follows the most common naming convention. With the number of objects we manipulate in our code nowadays, a 2 letters prefix is not enough. For somebody who reads your code, bx could be any type of box: ComboBox, TextBox, CheckBox. The 3 letters cbo makes it clearer that it is a ComboBox. Most programmer would use txt for a TextBox and chk for a CheckBox. That makes the code easier to understand later on.

Using an uppercase to separate the prefix from the use of the control makes it also easier to read. If you are in VB, you type the uppercase only in the property sheet for the ComboBox. In the code, forget about it. If you type everything in lowercase, the editor will automatically set the uppercase for you. If you are in C# however, you will have to be careful about the uppercase all the time because C# see cbosource and cboSource as 2 different variables.

When you fill a ComboBox with a DataTable each entry in the ComboBox represents a row in the table, not the text that the user sees on the screen. The SelectedItem is thus a DataRow object, and ToString on a DataRow returns "System.Data.DataRow". Since you have no course of that name, the query does not return any result.

What you need to retrieve to get the text selected by the user is the Text property of the ComboBox:

Code:

course = bxCourse.Text

-------

You are also making a basic mistake in your code, one that most programmers do. It's not your fault, all the damned books on the market show the stuff that way. They are wrong.

A DataSet is a container for tables, multiple tables, that have relations between them. The role of the DataSet is to maintain those relations and the integrity between the tables.

When you have only one Table, a DataSet is useless. It just eats up memory and makes it a little harder to work on your table. Here is what your code should look like:

ADODB.RecordSet were used in the old ADO, before .NET. This class is normally not used in .NET.

I suspect that your ConnectionString is not good. Maybe you copied an old ConnectionString from a VB6 application. ADO and ADO.NET ConnectionStrings are similar, but there are some differences.

Since we do not see the ConnectionString in your code, there is no way to tell why you are having this error.

-----

There is no SelectedItemChanged for a ComboBox. There is a SelectedIndexChanged however.

Most often, SelectedValueChanged and also SelectedIndexChanged are interchangeable since both are fired in most situations.

But for most cases SelectedIndexChanged is the best bet since it will trigger everytime the user makes a selection. In some situations, SelectedValue will not trigger. It depends on the type of data that fills the ComboBox, the way it was filled (through Add.Items or through DataSource), whether the ValueMember was set and the data in the field on which the ValueMember was set.

Can you please tell me one more thing...like how to select and store the value if its datatype is set to AutoNumber in the database.
like for example there is a table names "tutor_details" with following specifications
tutor_id as first column name primary key datatype="AutoNumber"
fullname as second column datatype="Text"

I am populating combo box - cmbtutor with fullname and then storing its respective tutor_id in a variable and then populating some other combo box for which results are based on search query on basis of tutor_id in other relational table.
Thanks