Q: multiple fields in dropdown list

G

Guest

Hello,
I am suing SELECT * from MyTable in a stored procedure and populate dropdown
list. By using followings:
ddlSP.DataSource = DS;
ddlSP.DataTextField = "PName";
ddlSP.DataValueField = "PID";
ddlSP.DataBind();

This show PName in dropdown list, It is working fine. Now I need to combine
PName, Field1 and Field2 as “PName: Field1 – Field2†and show it in the
dropdown list?
How should I do this?
Thanks,
Jim.
 
E

Elroyskimms

I'm fairly new so my syntax might be off, but I have done something
similar. You need to combine the field names in your select statement
and then assign them to an alias which you will use in your
DataTextField value.

Here's a quick sample:

SELECT PName + ': ' + Field1 + ' - ' + Field2 AS MyAlias FROM MyTable
....
ddlSP.DataTextField = "MyAlias";

The + operator concatenates the strings (similar to & in VB).
The AS keyword assigns the results to a new field name in the
datareader (also useful when accessing multiple fields with the same
name in one query).
Depending on the datatypes in the table, you may see some errors about
type conversions. You may need to convert one or more of the field
types using convert... something like this for converting to varchar:

convert(varchar(4),Field1)
(which converts the contents of Field1 into a varchar, length 4)
 
T

TDAVISJR

You can also do this manually for each item in the datasource.
For a Datareader

While dr.Read()

dropDownList1.Items.Add(New ListItem(dr("field1"), dr("field2")))

End While

The Add method takes in a String or a ListItem object.
 
T

TDAVISJR

Ooops..got my synthax incorrect. However, this is answered in a previous
post. See
Subject: Adding List Item Value
Date: 4/12

The correct synthax is shown there
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top