Solved

Multiple items selected in a DropDownList?

Posted on 2014-09-04
3
268 Views
Last Modified: 2014-09-08
How do I fix  my code to stop attempting to select multiple items from my drop down list. I'm needing to find an item in my drop down list and select that index when the item is found but my code is crashing for some reason and I can't figure out why.

code that crashes

            ListItem selectedListItem = ddlDepartment.Items.FindByText(dbr["ADEPT"].ToString().Trim());

            if (selectedListItem != null)
            {
                selectedListItem.Selected = true;
            }

            LoadPositionOrJobTitle("Marketing");
            selectedListItem = null;

            selectedListItem = ddlPosJobTitle.Items.FindByText(dbr["APOSITION"].ToString().Trim());

            if (selectedListItem != null)
            {
                selectedListItem.Selected = true;    <---crashes here
            }


my LoadPositionOrJobTitle method

private void LoadPositionOrJobTitle(string sDept)
    {
        ddlPosJobTitle.Items.Clear();

        if (sDept.Equals(""))
        {
            ddlPosJobTitle.Items.Add(new ListItem("Other", "0"));
        }
        #region CORP DEPARTMENT POSITION LISTINGS
        else if (pUserBranch.Equals("CORP"))
        {
            if (sDept.Equals("Accounting/Finance"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Director of Financial Planning & Analysis", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Controller", "2"));
                ddlPosJobTitle.Items.Add(new ListItem("Assistant Controller", "3"));
                ddlPosJobTitle.Items.Add(new ListItem("Accounting Specialist", "4"));
                ddlPosJobTitle.Items.Add(new ListItem("Accountant", "5"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "6"));

            }
            else if (sDept.Equals("Administrative"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Executive Assistant", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Administrative Assistant", "2"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "3"));
            }
            else if (sDept.Equals("Human Resources"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("President", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Executive Vice President & CFO", "2"));
                ddlPosJobTitle.Items.Add(new ListItem("VP Purchasing & Wholesale", "3"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "4"));
            }
            else if (sDept.Equals("Internal Control"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Business Analyst/Internal Control", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "2"));
            }
            else if (sDept.Equals("Logistics"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Director of Logistics", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "2"));
            }
            else if (sDept.Equals("Marketing"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Director of Marketing and Advertisement", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Marketing Manager", "2"));
                ddlPosJobTitle.Items.Add(new ListItem("Advertising Coordinator/Marketing", "3"));
                ddlPosJobTitle.Items.Add(new ListItem("Designer/Creative Manager", "4"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "5"));
            }
            else if (sDept.Equals("IT"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Director of Information Technology", "1"));
                ddlPosJobTitle.Items.Add(new ListItem(".NET Developer Analyst", "2"));
                ddlPosJobTitle.Items.Add(new ListItem("Network Administrator", "3"));
                ddlPosJobTitle.Items.Add(new ListItem("Senior PC/Web Analyst", "4"));
                ddlPosJobTitle.Items.Add(new ListItem("Senior AS400 Programer Analyst & Project", "5"));
                ddlPosJobTitle.Items.Add(new ListItem("System Support Specialist", "6"));
                ddlPosJobTitle.Items.Add(new ListItem("Telecommunications Administrator", "7"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "8"));
            }
            else if (sDept.Equals("Warranty"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Senior Manager, Truck Maintenance ", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Truck Maintenance Analyst", "2"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "3"));
            }
            else if (sDept.Equals("National Accounts"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("National Accounts Manager", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "2"));
            }
            else if (sDept.Equals("Operations"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Director of Operations", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("National Parts & Service Manager", "2"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "3"));
            }
            else if (sDept.Equals("Purchasing & Wholesale"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Assistant Branch Manager", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Buyer", "2"));
                ddlPosJobTitle.Items.Add(new ListItem("Regional Buyer", "3"));
                ddlPosJobTitle.Items.Add(new ListItem("Administrative Assistant", "4"));
            }
            else if (sDept.Equals("Sales Operations"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Director of Sales, Eastern Region", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Director of Sales, Western Region", "2"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "3"));
            }
            else if (sDept.Equals("Titling"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Inventory/Title Specialist", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Titling/Accounting Specialist", "2"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "3"));
            }
            else if (sDept.Equals("Training"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Director of Sales Training", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "2"));
            }
        }
        #endregion CORP

        #region TRANSPORT FUNDING DEPARTMENT POSITION LISTINGS
        else if (pUserBranch.Equals("TF"))
        {
            if (sDept.Equals("Administrative"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Office Manager", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Administrative Assistant", "2"));
                ddlPosJobTitle.Items.Add(new ListItem("Office Assistant", "3"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "4"));
            }
            else if (sDept.Equals("Executive"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("TF President", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "2"));
            }
            else if (sDept.Equals("Accounting/Finance"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Controller", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "2"));
            }
            else if (sDept.Equals("Collections"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("VP/Portfolio Manager", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Collections Supervisor", "2"));
                ddlPosJobTitle.Items.Add(new ListItem("Collections Specialist", "3"));
                ddlPosJobTitle.Items.Add(new ListItem("Asset Recovery Manager", "4"));
                ddlPosJobTitle.Items.Add(new ListItem("Bankruptcy & Litigation Specialist", "5"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "6"));

            }
            else if (sDept.Equals("Customer Service"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Customer Service Supervisor", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Customer Service Specialist", "2"));
                ddlPosJobTitle.Items.Add(new ListItem("Insurance Specialist", "3"));
                ddlPosJobTitle.Items.Add(new ListItem("Collateral Specialist", "4"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "5"));
            }
            else if (sDept.Equals("National Accounts"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("National Account Specialist", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "2"));
            }
            else if (sDept.Equals("Documentation and Funding"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Funding Manager", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Funding Specialist", "2"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "3"));
            }
            else if (sDept.Equals("Credit"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Chief Credit Officer", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Credit Manager", "2"));
                ddlPosJobTitle.Items.Add(new ListItem("Credit Specialist", "3"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "4"));
            }
            else if (sDept.Equals("Remarketing"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Remarketing Manager", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "2"));
            }
        }
        #endregion
        else if (!pUserBranch.Equals("NAT"))
        {
            if (sDept.Equals("Branch Titles"))
            {
                ddlPosJobTitle.Items.Add(new ListItem("Select", "0"));
                ddlPosJobTitle.Items.Add(new ListItem("Branch Manager", "1"));
                ddlPosJobTitle.Items.Add(new ListItem("Assistant Branch Manager", "2"));
                ddlPosJobTitle.Items.Add(new ListItem("Sales Manager", "3"));
                ddlPosJobTitle.Items.Add(new ListItem("Finance & Insurance Manager", "4"));
                ddlPosJobTitle.Items.Add(new ListItem("Inventory Coordinator", "5"));
                ddlPosJobTitle.Items.Add(new ListItem("Branch Administrative Assistant", "6"));
                ddlPosJobTitle.Items.Add(new ListItem("Retail Sales Associate", "7"));
                ddlPosJobTitle.Items.Add(new ListItem("Other", "8"));
            }
        }
        else
        {
            ddlPosJobTitle.Items.Add(new ListItem("Other", "0"));
        }

        ddlPosJobTitle.SelectedIndex = 0;
    }

Open in new window


multiple items selected
0
Comment
Question by:mikesExpertExchange
3 Comments
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 40304747
Try setting the selectedIndex to -1 before you set the item selected.


            if (selectedListItem != null)
            {
                ddlPosJobTitle.SelectedIndex = -1;
                selectedListItem.Selected = true;    <---crashes here
            }

the other way to do this is:

   if (selectedListItem != null)
            {
                ddlPosJobTitle.SelectedIndex = ddlPosJobTitle.Items.IndexOf(selectedListItem);
            }
0
 
LVL 1

Author Closing Comment

by:mikesExpertExchange
ID: 40306287
Thank you!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

12 Experts available now in Live!

Get 1:1 Help Now