473,503 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help - DropDownList Blues

Hello,

I have a DropDownList that I populate from an odbc datasource in code.
When I bring up the form for editing, how do I select the value in the
DropDownList based on a value stored in the database?

I tried:
lb1.SelectedItem.Text = dr["field"];
lb1.SelectedItem.Value = dr["field"];

Questions:
1. What happens if the item store in the database is no longer in the
dropdownlist box?
2. How can I sync two dropdownlist boxes?

When dropdownlist1 is selected, I fill dropdownlist2 when values from a
database based on the selected value in dropdownlist1.
When I bring the form up to edit a record, I want to select the value in
dropdownlist2 that matches the value in the database. This is not working
for me. The events are firing and the dropdownlists are filled, but the
value is not selected in the database.

Hope I explained this ok.

Thanks
Frank
using c#
Nov 15 '05 #1
2 1963
>how do I select the value in the DropDownList based on a value stored in
the database

ListItem li = ddYourList.Items.FindByValue("YourValue");
if (li != null)
li.Selected = true;

Be careful you do not try and set two listitems' selected property to true
at the same time, or you will get an error.

--
James J. Foster, DotNetCoders
http://www.dotnetcoders.com
"Frank Rocco" <fa*****@hotmail.com> wrote in message
news:uz**************@TK2MSFTNGP12.phx.gbl...
Hello,

I have a DropDownList that I populate from an odbc datasource in code.
When I bring up the form for editing, how do I select the value in the
DropDownList based on a value stored in the database?

I tried:
lb1.SelectedItem.Text = dr["field"];
lb1.SelectedItem.Value = dr["field"];

Questions:
1. What happens if the item store in the database is no longer in the
dropdownlist box?
2. How can I sync two dropdownlist boxes?

When dropdownlist1 is selected, I fill dropdownlist2 when values from a
database based on the selected value in dropdownlist1.
When I bring the form up to edit a record, I want to select the value in
dropdownlist2 that matches the value in the database. This is not working
for me. The events are firing and the dropdownlists are filled, but the
value is not selected in the database.

Hope I explained this ok.

Thanks
Frank
using c#

Nov 15 '05 #2
Thanks again James,

Is it better to do it your way (method 1) over method 2 for robustness?
method 1
ListItem li = lbROOM_BED.Items.FindByValue(dr["ROOM_BED"].ToString());
if (li != null)
li.Selected = true;

method 2
lbROOM_BED.SelectedIndex =
lbROOM_BED.Items.IndexOf(lbROOM_BED.Items.FindByTe xt(dr["ROOM_BED"].ToString
()));
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3150
by: Frank | last post by:
Hi, we are using oracle clients (Release 9.0.1.0.1 - Production) on an NT4 (Service Pack6) computers. the server is a W2K, (Oracle9i Enterprise Edition Release 9.0.1.1.1 - Production With the...
7
2159
by: Stephen | last post by:
I have some code which I call from a custom validator however I seem to have got the logic wrong and im having trouble figuring out how to write my code to get things to work the way I require....
10
10710
by: dhnriverside | last post by:
Hi guys Still having a problem with this dropdownlist. Basically, I've got 4. The first 2 work fine, then my code crashes on the 3rd. ddlEndTimeHour.Items.FindByValue(endTime).Selected =...
5
2744
by: Siva | last post by:
Hello I have a dropdownlist inside the gridview as a template column defined as follows: <asp:TemplateField HeaderText="Choose Location"> <ItemTemplate> <asp:DropDownList ID="ddlChooseLoc"...
0
2385
by: Mike Collins | last post by:
I someone can please help, I am about at an end in trying to figure this out. I am adding some dynamic controls to my page (I found out that I was supposed to be doing that in the oninit event,...
0
1457
by: ana_gil | last post by:
hello i have a problem with a dropdown list i am populating from an Access table. I´ve seen this problem in other discussions but i can´t seem to find the solution. please help. my .aspx has...
4
8534
by: pankajsingh5k | last post by:
Hi guys, These question is for all the experts... Please help me before my brain explodes The problem is again with the formview control.. I have a formview and i have to use it that...
10
4612
by: yeshello54 | last post by:
Hey guys i am pretty new to java swing and need some help. I am developing a simple color chooser program in swing. I have a color panel that is connected to three sliders. red green and blue. but...
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7072
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
7271
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,...
1
6979
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...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.