By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

Q: multiple fields in dropdown list

P: n/a
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.
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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)

Nov 19 '05 #2

P: n/a
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.

--
TDAVISJR
aka - Tampa.NET Koder
"Elroyskimms" <el*********@yahoo.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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)

Nov 19 '05 #3

P: n/a
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

--
TDAVISJR
aka - Tampa.NET Koder
"Elroyskimms" <el*********@yahoo.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
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)

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.