473,396 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Q: multiple fields in dropdown list

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
3 3137
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jas | last post by:
I want an ASP page with a dropdown and a simple button. Every time the user chooses an item from the dropdown and clicks on the button i want that value written below in list and allow user to...
2
by: Smythe32 | last post by:
Hi, I have this code below that will add a new item to a combo box if it is not in the combo box. I also would like the user to add the description for the new addition to the combo box and...
0
by: rayone | last post by:
Hi folks. I need advice. 2 options, which do you think is the better option to display/retrieve/report on the data. Keep in mind reporting (Crystal), SQL Performance, VB Code, usability,...
2
by: Harry | last post by:
Hi, Quick question regarding dropdownlists. On one page I have 10 asp:dropdownlists. The user selects an option from a dropdown list and then it updates an asp:label with the relevant text....
4
by: jayscott1 | last post by:
What I would like to do is have the user start typing in the last name and as they were typing it would retrieve names from the database and give a drop down like autosuggest in google. That much...
1
by: jjuan | last post by:
I have a multiple dropdown which have multiple selection. If I select multiple value on my multiple dropdown list and submit it,how can i write it on the tblotherlang sample code <select...
1
by: tech.fors | last post by:
Hi , How to make multiple select in dropdown list box using asp and access can anyone help me regarding this. thanking u in advance, Regards, tech.fors
8
by: Wingot | last post by:
Hey, I have a program I am trying to write using Visual C#, SQL Server 2005/2008, and Visual Studio 2008, and one part of it includes a Schema called Client. Inside this schema, three tables...
2
by: permander kumar | last post by:
hi plz help me, i have a code in which two multiple selection dropdown list. in first list if I am select single value the data are fetch in second table on button click. but the problem is in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.