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


I looked at some resoureces before asking the question....but couldn't do it ...so 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"



Who is Participating?
RejojohnyConnect With a Mentor Commented:
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

assuming course_id and course_name are both Varchar fields

        coursesddl.DataTextField = "Course_Name"
            coursesddl.DataValueField = "COURSE_ID"
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
_EsamAuthor Commented:
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)..

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.