problems with DropDownList.SelectedValue

M

musosdev

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.
 
D

dotNetDave

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(ClientDropDownList.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
 
M

musosdev

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 said:
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(ClientDropDownList.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 said:
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.
 
D

dotNetDave

Have you doubled checked during runtime that the value coming from your db is
actually in the ComboBox?

David

musosdev said:
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 said:
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(ClientDropDownList.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 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top