473,403 Members | 2,354 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,403 software developers and data experts.

Dropdown List - Check if value exists in list prior to selection

Hello.

I am reading a value from a table and trying to determine if that value
exists in a list of values associated with a dropdownlist. If so, I select
the value, otherwise, I don't. I haven't been able to figure this operation
out so far.

Code:

if (reader["region_id"] != System.DBNull.Value)
{
if
(ddlApplicantRM.Items.Contains(ddlApplicantRM.Item s.FindByValue(reader["region_id"].ToString())) == true)
{
ddlApplicantRM.SelectedValue =
reader["region_id"].ToString();
}
else
{
ddlApplicantRM.SelectedValue = "0";
}
}

Even though the value clearly exists in the list of values attached to the
dropdown, it never selects the value in the dropdown correctly. Is this the
proper way of doing what I am trying to accomplish? Does it matter if
region_id is an INT and the comparison function requires a string?

Thanks in advance for any suggestions or help.
Mar 10 '07 #1
5 27296
Hi,
Try either of this code:

Method 1 :
DropDownList1.SelectedIndex =
DropDownList1.Items.IndexOf(DropDownList1.Items.Fi ndByValue(reader["region_id"].ToString()));

//DropDownList1.SelectedIndex =
DropDownList1.Items.IndexOf(DropDownList1.Items.Fi ndByText(<Text>));

or

Method 2 :
DropDownList1.Items.FindByValue(reader["region_id"].ToString()).Selected =
true ;

//DropDownList1.Items.FindByText("<Text>").Selected = true ;

If my answer helped you,then please do press Yes below.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"Brenden Bixler" wrote:
Hello.

I am reading a value from a table and trying to determine if that value
exists in a list of values associated with a dropdownlist. If so, I select
the value, otherwise, I don't. I haven't been able to figure this operation
out so far.

Code:

if (reader["region_id"] != System.DBNull.Value)
{
if
(ddlApplicantRM.Items.Contains(ddlApplicantRM.Item s.FindByValue(reader["region_id"].ToString())) == true)
{
ddlApplicantRM.SelectedValue =
reader["region_id"].ToString();
}
else
{
ddlApplicantRM.SelectedValue = "0";
}
}

Even though the value clearly exists in the list of values attached to the
dropdown, it never selects the value in the dropdown correctly. Is this the
proper way of doing what I am trying to accomplish? Does it matter if
region_id is an INT and the comparison function requires a string?

Thanks in advance for any suggestions or help.
Mar 11 '07 #2
I appreciate you taking a stab at the solution, both both failed.

The first simply did not work. The second causes the page to render
incorrectly.

"Manish Bafna" wrote:
Hi,
Try either of this code:

Method 1 :
DropDownList1.SelectedIndex =
DropDownList1.Items.IndexOf(DropDownList1.Items.Fi ndByValue(reader["region_id"].ToString()));

//DropDownList1.SelectedIndex =
DropDownList1.Items.IndexOf(DropDownList1.Items.Fi ndByText(<Text>));

or

Method 2 :
DropDownList1.Items.FindByValue(reader["region_id"].ToString()).Selected =
true ;

//DropDownList1.Items.FindByText("<Text>").Selected = true ;

If my answer helped you,then please do press Yes below.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"Brenden Bixler" wrote:
Hello.

I am reading a value from a table and trying to determine if that value
exists in a list of values associated with a dropdownlist. If so, I select
the value, otherwise, I don't. I haven't been able to figure this operation
out so far.

Code:

if (reader["region_id"] != System.DBNull.Value)
{
if
(ddlApplicantRM.Items.Contains(ddlApplicantRM.Item s.FindByValue(reader["region_id"].ToString())) == true)
{
ddlApplicantRM.SelectedValue =
reader["region_id"].ToString();
}
else
{
ddlApplicantRM.SelectedValue = "0";
}
}

Even though the value clearly exists in the list of values attached to the
dropdown, it never selects the value in the dropdown correctly. Is this the
proper way of doing what I am trying to accomplish? Does it matter if
region_id is an INT and the comparison function requires a string?

Thanks in advance for any suggestions or help.
Mar 11 '07 #3
On Mar 11, 7:36 pm, Brenden <Bren...@discussions.microsoft.comwrote:
I appreciate you taking a stab at the solution, both both failed.

The first simply did not work. The second causes the page to render
incorrectly.

"Manish Bafna" wrote:
Hi,
Try either of this code:
Method 1 :
DropDownList1.SelectedIndex =
DropDownList1.Items.IndexOf(DropDownList1.Items.Fi ndByValue(reader["region_*id"].ToString()));
//DropDownList1.SelectedIndex =
DropDownList1.Items.IndexOf(DropDownList1.Items.Fi ndByText(<Text>));
or
Method 2 :
DropDownList1.Items.FindByValue(reader["region_id"].ToString()).Selected =
true ;
//DropDownList1.Items.FindByText("<Text>").Selected = true ;
If my answer helped you,then please do press Yes below.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.
"Brenden Bixler" wrote:
Hello.
I am reading a value from a table and trying to determine if that value
exists in a list of values associated with a dropdownlist. If so, I select
the value, otherwise, I don't. I haven't been able to figure this operation
out so far.
Code:
if (reader["region_id"] != System.DBNull.Value)
{
if
(ddlApplicantRM.Items.Contains(ddlApplicantRM.Item s.FindByValue(reader["reg*ion_id"].ToString())) == true)
{
ddlApplicantRM.SelectedValue =
reader["region_id"].ToString();
}
else
{
ddlApplicantRM.SelectedValue = "0";
}
}
Even though the value clearly exists in the list of values attached to the
dropdown, it never selects the value in the dropdown correctly. Is this the
proper way of doing what I am trying to accomplish? Does it matter if
region_id is an INT and the comparison function requires a string?
Thanks in advance for any suggestions or help.- Hide quoted text -

- Show quoted text -
ddlApplicantRM.Items.FindByValue(reader["reg*
ion_id"].ToString()).Selected = true;

should work! If it doesn't - check what value the reader has there.

try {
ddlApplicantRM.Items.FindByValue(reader["reg*
ion_id"].ToString()).Selected = true;
} catch {
ddlApplicantRM.SelectedValue = "0";
}

Regarding your code

Items.Contains requires a ListItem, so:

if (ddlApplicantRM.Items.Contains(new ListItem("...text here...",
reader["reg*ion_id"].ToString()))) {
...........

where "...text here..." is the text of the list item, I don't know if
it's the same as reader["reg*ion_id"], or not...
Mar 11 '07 #4
Hi,
Last thing you can try is using Trim():
ddlApplicantRM.Items.FindByValue(reader["reg-
ion_id"].ToString().Trim()).Selected = true;
please let me know if now it is working or not
--
If my answer helped you,then please do press Yes below.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"Alexey Smirnov" wrote:
On Mar 11, 7:36 pm, Brenden <Bren...@discussions.microsoft.comwrote:
I appreciate you taking a stab at the solution, both both failed.

The first simply did not work. The second causes the page to render
incorrectly.

"Manish Bafna" wrote:
Hi,
Try either of this code:
Method 1 :
DropDownList1.SelectedIndex =
DropDownList1.Items.IndexOf(DropDownList1.Items.Fi ndByValue(reader["region_-id"].ToString()));
//DropDownList1.SelectedIndex =
DropDownList1.Items.IndexOf(DropDownList1.Items.Fi ndByText(<Text>));
or
Method 2 :
DropDownList1.Items.FindByValue(reader["region_id"].ToString()).Selected =
true ;
//DropDownList1.Items.FindByText("<Text>").Selected = true ;
If my answer helped you,then please do press Yes below.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.
"Brenden Bixler" wrote:
Hello.
I am reading a value from a table and trying to determine if that value
exists in a list of values associated with a dropdownlist. If so, I select
the value, otherwise, I don't. I haven't been able to figure this operation
out so far.
Code:
if (reader["region_id"] != System.DBNull.Value)
{
if
(ddlApplicantRM.Items.Contains(ddlApplicantRM.Item s.FindByValue(reader["reg-ion_id"].ToString())) == true)
{
ddlApplicantRM.SelectedValue =
reader["region_id"].ToString();
}
else
{
ddlApplicantRM.SelectedValue = "0";
}
}
Even though the value clearly exists in the list of values attached to the
dropdown, it never selects the value in the dropdown correctly. Is this the
proper way of doing what I am trying to accomplish? Does it matter if
region_id is an INT and the comparison function requires a string?
Thanks in advance for any suggestions or help.- Hide quoted text -
- Show quoted text -

ddlApplicantRM.Items.FindByValue(reader["reg-
ion_id"].ToString()).Selected = true;

should work! If it doesn't - check what value the reader has there.

try {
ddlApplicantRM.Items.FindByValue(reader["reg-
ion_id"].ToString()).Selected = true;
} catch {
ddlApplicantRM.SelectedValue = "0";
}

Regarding your code

Items.Contains requires a ListItem, so:

if (ddlApplicantRM.Items.Contains(new ListItem("...text here...",
reader["reg-ion_id"].ToString()))) {
...........

where "...text here..." is the text of the list item, I don't know if
it's the same as reader["reg-ion_id"], or not...
Mar 12 '07 #5
None of the solutions provided previously worked, but I finally figured it
out. Thanks to all for helping me reach success.

On the front-end page (file.aspx), I have the dropdown control bound to a
SQL Data Source. The portion of my code that checks for the value in the
dropdown list (in the code behind, file.aspx.cs) is always encountering an
empty dropdown list, because the code to pull the values into the dropdown
list hasn't yet executed.

To fix, I did the following:

// nullify the existing connection
ddlApplicantRM.DataSourceID = null;

// reconnect in the code-behind
ddlApplicantRM.DataSource = sqlRM2;

// force the dropdown to load the items
ddlApplicantRM.DataBind();

// Now, check for the value against a full list of values
try
{

ddlApplicantRM.Items.FindByValue(reader["region_id"].ToString().Trim()).Selected = true;
}
catch
{
ddlApplicantRM.SelectedValue = "0";
}
Mar 20 '07 #6

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

Similar topics

2
by: dixie | last post by:
I am trying to write some VBA that can check if a certain value exists in a field in a table. The field is a text field. The table is not a part of the query that forms the recordsource of the...
1
by: Stu | last post by:
Hi, I have a web form that uses simple binding to bind properties to the controls on the page. However, if I try and bind a value to a dd list using: SelectedValue="<%# MyProperty %>" If...
1
by: James | last post by:
vb.net 2003 i wrote a windows service that does threading. My codes are a) the service thread will read a list of machine from a text file (machines.txt). b) it then logon using the below...
10
by: codefire | last post by:
Hi, I have some simple code - which works...kind of..here's the code: import os def print_tree(start_dir): for f in os.listdir(start_dir): fp = os.path.join(start_dir, f)
2
by: desilgalis | last post by:
I am looking for some help to reset a drop down list to its original option value each time a selection is made. My drop down is as follows: <p><font face="Trebuchet MS"><b><font...
5
by: Andrew Meador | last post by:
I have a form (Change Card List by Status) with a check box (cboNOT) and a list box (lstStatus). There is an Open Report button that opens a report (Report - Change Card List) which uses a query...
10
by: Dooza | last post by:
Using ASP with VBScript is there a clever way to easily check if a value exists in an array without looping through it? Steve
1
by: AarthiVedhavalli | last post by:
Hi, In my C# web application I have a list box whic is binded to database. What i need now is , if i double click a value in list box that vale should be added to a dropdown list.. Any...
2
by: qwedster | last post by:
Folk! How to programattically check if null value exists in database table (using stored procedure)? I know it's possble in the Query Analyzer (see last SQL query batch statements)? But how...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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 project—planning, coding, testing,...
0
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...

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.