problems with DropDownList.SelectedValue

  • Thread starter Thread starter musosdev
  • Start date Start date
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.
 
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
 
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.
 
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.
 
Back
Top