Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop Down list using two fields

Status
Not open for further replies.

sthmpsn1

MIS
Sep 26, 2001
456
US
I want to display information from a database into a dropdown list. I want the option shown to come from two database fields.
This is what I have
Dim dbconn As SqlConnection = New SqlConnection("server=AM1ST_FS1;database=HRINFO;uid=sa;")
Dim selectCMD As SqlCommand = New SqlCommand()
selectCMD.CommandText = "Select * from employeeinfo"
selectCMD.Connection = dbconn
selectCMD.CommandTimeout = 30
dbconn.Open()
employee.DataSource = selectCMD.ExecuteReader()
employee.DataTextField = "firstName"
employee.DataValueField = "loginID"
employee.DataBind()
dbconn.Close()

I want the dataTextField to be something like this
employee.DataTextField = "firstName" & " " & "lastName"
Obviously this doesn't work. Any idea's on how to make it work???
 
It should be something like this. Note I never tested this at all but it should point you in the right direction...
Add this to your code, and remove the lines you had where you set employee object to a datasource, etc.


dim sqlData as sqlclient.sqldatareader
dim addString as String = ""
dim employeeItem as ListItem

sqldata = selectCMD.ExecuteReader()
while sqldata.read()
for i=0 to sqlData.fieldcount-1
addString &= sqlData(i).tostring() & " "
next
employeeItem = New ListItem(addString, loginId)
employee.items.add(employeeItem)
addString = ""
End While


Good Luck... Life is like a box of chocolates, sweet
 
You could loop through your dataset and manually add each item.

for each row in dataset.tablename
dim mystring as string
mystring = row.firstname & " " & row.lastname
dropdown.items.add(mystring)

mystring = nothing

Next


Or you could combine the two fields in your select statment so that they come back as one.

select (select FName, LName from employeeinfo) as name, other fields from employeeinfo


Take your pick both would work. That'l do donkey, that'l do
[bravo]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top