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

problems with DropDownList.SelectedValue

Hi

I'm trying to set the selected item on a dropdownlist on my c# asp.net page.
Here's the code...

private void Page_Load(object sender, EventArgs e)
{
setuplist();
ddlPrimaryAOW.SelectedValue = sqlDR["PrimaryAOW"].ToString();
}

The SelectedValue statement fails with an indexoutofrange exception.

My setuplist() function adds items to the list, like so...

ddlPrimaryAOW.Items.Add(new listitem("item1", "i1"));

so I would expect if sqlDR["PrimaryAOW"].ToString() = "i1", then the code
should work?! I know that the list is populating correctly, and that one of
my items has the value "i1", and I know the result of
sqlDR["PrimaryAOW"].ToString() also = "i1".

What am I doing wrong?!

Thx, dan.
Sep 30 '08 #1
3 1885
What your doing is not very "safe" because if the value you are trying to set
in the ComboBox is not there it will cause an Exception. Here is a better way:

ClientDropDownList.SelectedIndex =
ClientDropDownList.Items.IndexOf(ClientDropDownLis t.Items.FindByValue(clientId));

Just replace "clientId" with the value from your DataSet record.

David

======================================
David McCarter [Microsoft MVP]
www.dotNetTips.com
David McCarter''''s .NET Coding Standards available at:
http://www.cafepress.com/geekmusicart.1654787045
"musosdev" wrote:
Hi

I'm trying to set the selected item on a dropdownlist on my c# asp.net page.
Here's the code...

private void Page_Load(object sender, EventArgs e)
{
setuplist();
ddlPrimaryAOW.SelectedValue = sqlDR["PrimaryAOW"].ToString();
}

The SelectedValue statement fails with an indexoutofrange exception.

My setuplist() function adds items to the list, like so...

ddlPrimaryAOW.Items.Add(new listitem("item1", "i1"));

so I would expect if sqlDR["PrimaryAOW"].ToString() = "i1", then the code
should work?! I know that the list is populating correctly, and that one of
my items has the value "i1", and I know the result of
sqlDR["PrimaryAOW"].ToString() also = "i1".

What am I doing wrong?!

Thx, dan.
Sep 30 '08 #2
David,

I've changed my code to match yours, and I still get an IndexOutOfRange
exception.

Doesn't make sense, seen as the items in the list are populated by values
that I know are present in the sqlDr records.

Any clues?

"dotNetDave" wrote:
What your doing is not very "safe" because if the value you are trying to set
in the ComboBox is not there it will cause an Exception. Here is a better way:

ClientDropDownList.SelectedIndex =
ClientDropDownList.Items.IndexOf(ClientDropDownLis t.Items.FindByValue(clientId));

Just replace "clientId" with the value from your DataSet record.

David

======================================
David McCarter [Microsoft MVP]
www.dotNetTips.com
David McCarter''''s .NET Coding Standards available at:
http://www.cafepress.com/geekmusicart.1654787045
"musosdev" wrote:
Hi

I'm trying to set the selected item on a dropdownlist on my c# asp.net page.
Here's the code...

private void Page_Load(object sender, EventArgs e)
{
setuplist();
ddlPrimaryAOW.SelectedValue = sqlDR["PrimaryAOW"].ToString();
}

The SelectedValue statement fails with an indexoutofrange exception.

My setuplist() function adds items to the list, like so...

ddlPrimaryAOW.Items.Add(new listitem("item1", "i1"));

so I would expect if sqlDR["PrimaryAOW"].ToString() = "i1", then the code
should work?! I know that the list is populating correctly, and that one of
my items has the value "i1", and I know the result of
sqlDR["PrimaryAOW"].ToString() also = "i1".

What am I doing wrong?!

Thx, dan.
Sep 30 '08 #3
Have you doubled checked during runtime that the value coming from your db is
actually in the ComboBox?

David

"musosdev" wrote:
David,

I've changed my code to match yours, and I still get an IndexOutOfRange
exception.

Doesn't make sense, seen as the items in the list are populated by values
that I know are present in the sqlDr records.

Any clues?

"dotNetDave" wrote:
What your doing is not very "safe" because if the value you are trying to set
in the ComboBox is not there it will cause an Exception. Here is a better way:

ClientDropDownList.SelectedIndex =
ClientDropDownList.Items.IndexOf(ClientDropDownLis t.Items.FindByValue(clientId));

Just replace "clientId" with the value from your DataSet record.

David

======================================
David McCarter [Microsoft MVP]
www.dotNetTips.com
David McCarter''''s .NET Coding Standards available at:
http://www.cafepress.com/geekmusicart.1654787045
"musosdev" wrote:
Hi
>
I'm trying to set the selected item on a dropdownlist on my c# asp.net page.
Here's the code...
>
private void Page_Load(object sender, EventArgs e)
{
setuplist();
ddlPrimaryAOW.SelectedValue = sqlDR["PrimaryAOW"].ToString();
}
>
The SelectedValue statement fails with an indexoutofrange exception.
>
My setuplist() function adds items to the list, like so...
>
ddlPrimaryAOW.Items.Add(new listitem("item1", "i1"));
>
so I would expect if sqlDR["PrimaryAOW"].ToString() = "i1", then the code
should work?! I know that the list is populating correctly, and that one of
my items has the value "i1", and I know the result of
sqlDR["PrimaryAOW"].ToString() also = "i1".
>
What am I doing wrong?!
>
Thx, dan.
Sep 30 '08 #4

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

Similar topics

4
by: starwiz | last post by:
I'm trying to use the DataGrid's editing with a DropDownList. I've tried using every code sample I've seen and none of them seem to be able to solve my problem. When I call...
15
by: Swetha | last post by:
Hello I have a DropDownList that I am populating using the following SqlDataSource: <asp:DropDownList ID="parentIDDropDownList" runat="server" DataSourceID="SqlDataSource3"...
6
by: tfsmag | last post by:
I have a Grid that I've set up to have two of the fields use dropdownlists while in edit mode. Now I can bind the data to the dropdownlists just fine. My problem is that the second dropdown depends...
2
by: Sean Chapman | last post by:
I've got a simple page I've made with a few controls (Treeview, Dropdownlist, ..). I'm running into an issue with autopostback. If I set it to AutoPostBack = "True", when the event should fire I...
3
by: Carlos Lozano | last post by:
Hello, I am having a problem getting the selectedValue from a dropdownlist that is populated with a dataReader and just can't see the problem. I did the following: dim dr as DataReader dr...
1
by: clickon | last post by:
Forget about the controlParameter for the moment, for testing purposes i have created the following Markup: <asp:Table ID="tblSelectRoute" runat="server" CssClass="asp-table"> <asp:TableRow>...
11
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
2
by: sree reddy | last post by:
..cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls;
6
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a gridview control with a template column that has a textbox and when the control is bound to the datasource the textbox is filled ok. I then change what is in the textbox in the gridview...
1
by: Brett | last post by:
I have a DropDownList in an ASP.NET web form that is populated with items from a lookup table by binding that DropDownList to a SqlDataSource. However, the items in the lookup table can change over...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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:
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
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
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
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
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,...

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.