[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1729
  • Last Modified:

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

Hi,

.
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
            ccddl.Open()
            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"


            coursesddl.DataBind()
            ccddl.Close()

Thanks..

_Esam
0
_Esam
Asked:
_Esam
  • 3
1 Solution
 
RejojohnyCommented:
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"
0
 
RejojohnyCommented:
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
0
 
_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...

like:
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)..

Thanks.
_Esam
0
 
RejojohnyCommented:
what is the max length for the course id? assuming it is 6
use this query
SELECT COURSE_ID,
          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.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now