Combo Box Help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: Combo Box Help

Hybrid View

  1. #1
    Join Date
    Oct 2010
    Location
    Auckland, NZ
    Posts
    9

    Question 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!!

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Welcome to DevX
    Quote Originally Posted by karan.khanna View Post
    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.
    What code do you have for this part, and what happens when you run the code?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    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.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  4. #4
    Join Date
    Oct 2010
    Location
    Auckland, NZ
    Posts
    9

    Exclamation Code for the application

    hey..dats my code for the query I asked...
    Code:
    Private Sub frmCreateNew1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            conn = connectdb.connection
            conn.Open()
            Me.MdiParent = frmParent
            Me.Dock = DockStyle.Fill
            bxLevel.Enabled = False
            bxModule.Enabled = False
            Dim select_query As New OleDbCommand("select_query", conn)
            Dim ds As New DataSet
            select_query.CommandText = "SELECT DISTINCT course_name FROM course_details"
            da = New OleDbDataAdapter(select_query)
            da.Fill(ds, "course_details")
            With bxCourse
                .DataSource = ds.Tables("course_details")
                .DisplayMember = "course_name"
            End With
        End Sub
    
     Private Sub bxCourse_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles bxCourse.SelectedValueChanged
            Dim data_query As New OleDbCommand("data_query", conn)
            Dim ds_1 As New DataSet
            Dim course As String
            course = bxCourse.SelectedItem.ToString()
            data_query.CommandText = "SELECT DISTINCT course_level FROM course_details WHERE course_name='" & course & "'"
            da = New OleDbDataAdapter(data_query)
            da.Fill(ds_1)
            If ds_1.Tables(0).Rows.Count = 0 Then
                bxLevel.Enabled = False
                MessageBox.Show("No records found", "TTMS Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Else
                bxLevel.Enabled = True
                bxLevel.DataSource = ds_1.Tables(0)
                bxLevel.DisplayMember = ds_1.Tables(0).Columns("course_level").ToString
            End If
        End Sub
    the project is running. its showing the values in bxcourse but showing the message No records found as the form loads....
    Last edited by Hack; 10-29-2010 at 11:21 AM. Reason: Added Code Tags

  5. #5
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    There lies the problem:

    Code:
    course = bxCourse.SelectedItem.ToString()
    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:

    Code:
    Dim dt As New DataTable
    select_query.CommandText = "SELECT DISTINCT course_name FROM course_details"
    da = New OleDbDataAdapter(select_query)
    da.Fill(dt, "course_details")
    With bxCourse
    .DataSource = dt
    That would be faster and more efficient as far as memory is concerned. And you can see that the syntax used to manipulate the table is simpler: dt vs ds.Tables("course_details")
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  6. #6
    Join Date
    Oct 2010
    Location
    Auckland, NZ
    Posts
    9
    hie...
    Thank you for your consideration

    as you said I have used datatable instead of dataset but its showing this error on the da.fill statement
    "Object is not an ADODB.RecordSet or an ADODB.Record.
    Parameter name: adodb"

    and to display the values of bxlevel I should write its code in which event of bxcourse. SelectedValueChanged or SelectedItemChange event?

  7. #7
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    Strange error.

    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.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  8. #8
    Join Date
    Oct 2010
    Location
    Auckland, NZ
    Posts
    9
    thanks you sir....the combo box problem is solved!!

    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

  9. #9
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    Initialize the ComboBox with the following code:

    Code:
    With cboTutor 'Change the name to fit
        .DataSource = ds.Tables("tutor_details")
        .DisplayMember = "fullname"
        .ValueMember = "tutor_id"
    End With
    After the user has made has selection, such as in the SelectedIndexChanged event, you can retrieve the name and the ID this way:

    Code:
    Dim name As String
    Dim tutorID as Integer 'or any type you use for your tutor_id field
    
    name = cboTutor.Text
    tutorID = cboTutor.SelectedValue
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  10. #10
    Join Date
    Oct 2010
    Location
    Auckland, NZ
    Posts
    9
    thanks you sir....its running fine...

  11. #11
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    I am very glad that I could help you, and your "thanks you" are very well received and appreciated.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

Similar Threads

  1. Replies: 1
    Last Post: 04-09-2008, 07:28 AM
  2. Help with .Net combo box
    By jherndon in forum .NET
    Replies: 1
    Last Post: 12-23-2005, 11:05 AM
  3. Query help in Access Combo Box
    By Larry Muller in forum VB Classic
    Replies: 1
    Last Post: 02-24-2001, 10:12 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center