Populate a drop down list (with two data columns)? VB.NET

Posted on 2005-05-08
Last Modified: 2008-01-09

I looked at some resoureces before asking the question....but couldn't do it asking ....
I need to populate a ddl from a database...

"I would like to show two columns from a database to the ddl"

????????IS IT POSSIBLE?????????????

My my code below so far attempted to populate the ddl with just one column..

let me know how i can make it to show two columms from the database...

Here is what I tried:

My ddl is:

<asp:DropDownList id="coursesddl" Runat="server"></asp:DropDownList>

My ddl databing code:

Dim ccddl As New OleDbConnection
            Dim sddl As String = Server.MapPath("UST.mdb")
            ccddl.ConnectionString = "provider =Microsoft.Jet.OLEDB.4.0; data source = " + sddl
            Dim daddl As New OleDbDataAdapter("SELECT COURSE_ID FROM COURSES ", ccddl)

            Dim dsddl As New DataSet
            daddl.Fill(dsddl, "COURSES")

            coursesddl.DataSource = dsddl.Tables("COURSES").DefaultView

            coursesddl.DataTextField = "COURSE_ID"
            coursesddl.DataValueField = "COURSE_ID"



Question by:_Esam
    LVL 26

    Accepted Solution

    u cannot show 2 columns in a dropdown .. but u could change ur query to join 2 columns to one column and attach it to the datatextfield property of the dropown .. in the code that u have given, u haven't stated the 2 colmns that u want to show .. so just assuming u have another column named "Course_Name", the query would be somethin like this
    SELECT COURSE_ID, COURSE_ID + course_Name as  Course_Name FROM COURSES

    assuming course_id and course_name are both Varchar fields

            coursesddl.DataTextField = "Course_Name"
                coursesddl.DataValueField = "COURSE_ID"
    LVL 26

    Expert Comment

    sorry u would naturally like some space or a delimeter between the 2 columns .. right? .. so the query has to be
    SELECT COURSE_ID, COURSE_ID + '  - ' + course_Name as  Course_Name FROM COURSES

    Author Comment

    i had some problem earlier to get the right results due to some property error for this part > daddl.Fill(dsddl, "COURSES") .

    it's working fine now except that when i concaneted two columns, the result somehow looks not so attractive...

    CS510 - This is course A
    CS525T - This is course B with a T
    CS625 - This is course C with no T.

    Would have been nice if all the "This" were aligned nicely, vertically ||||

    Let me know if you might have any suggestion on this (any way around)..

    LVL 26

    Expert Comment

    what is the max length for the course id? assuming it is 6
    use this query
              left(convert(varchar(6), COURSE_ID) + '      ', 6)  + '  - ' + course_Name as  Course_Name FROM COURSES

    the number spaces in the
    left(convert(varchar(6), COURSE_ID) + '      ', 6)
    statement = 6 i.e. the max digits of the course id ...

    with this statement the data will look like
    CS510  - This is course A
    CS525T- This is course B with a T
    CS625  - This is course C with no T.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now