Set SelectedIndex of DropDownList based on return value from database

Posted on 2006-04-18
Last Modified: 2008-03-10

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.

Question by:SWRO
    LVL 13

    Accepted Solution

    Try this:

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

    Author Comment

    That worked! Excellent.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now