Set SelectedIndex of DropDownList based on return value from database


I am trying to set the SelectedIndex of a DropDownList based upon a value that is returned from a Stored Procedure. The problem is that my current method doesn't account for a different # of items in the DropDownList.

Here's the basic setup:

ddlUserRoles - filled by a SQL SPROC that returns between 2 and 4 items based upon the role of the user visiting the page. An administrator would see 4 options while a 1st level Manager would only see 2, etc. Currently, I use a Case statement that assigns the SelectedIndex based upon the value I get out of the call.

Example: Joe is a 1st level manager.  He is going to edit the properties of one of his employees, Jane. The two possible RoleID options for Jane are 2 and 3. The DDL is populated with these two values. Janes' current RoleID is 3, so I would like for the RoleID of 3 record to get the SelectedIndex.

Is there some sort of FindByControl.value that will accomplish the same? And if so, any examples would be very helpful.  By the way, this is code.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try this:

Dim iSelected as integer = Cint(ddlUserRoles.SelectItem.Value)
DDLTWO.Items.FindByValue(iSelected).Selected = True

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SWROAuthor Commented:
That worked! Excellent.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.