DropDownList SelectedValue returning "out of the range"

  • Thread starter Thread starter Brent
  • Start date Start date
B

Brent

Here's my function:

public void getInterestLevelDDL(string co_interest_level)
{
sql = [valid sql];

//get_select_list creates a valid ICollection datatable
ctrlDDL_CIL.DataSource = tools.get_select_list(sql);


ctrlDDL_CIL.DataTextField = "value";
ctrlDDL_CIL.DataValueField = "key";
try
{
ctrlDDL_CIL.SelectedValue = co_interest_level;
ctrlDDL_CIL.DataBind();

}
catch
{
ctrlDDL_CIL.SelectedValue = "0";
ctrlDDL_CIL.DataBind();

}

}

If co_interest_level is in the range of dropdownlist values, I've no
problems. However, sometimes it's not, but I can't seem to get the code
to return a default gracefully. This example fails on the second
DataBind() with the error "Specified argument was out of the range of
valid values. Parameter name: value". However, the value "0" is ALWAYS
in the list, as I've made it part of the get_select_list return value.
Also, I've tried a ClearSelection(), but that doesn't seem to do a thing...

I'd sure appreciate any help!

--Brent
 
Hi,

You have the DataBind incorrectly , it should be:
try
{
ctrlDDL_CIL.DataBind();
ctrlDDL_CIL.SelectedValue = co_interest_level;
}


And remove the DataBind in the catch block


cheers,
 

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

Back
Top