Solved

Multiple items selected in a DropDownList?

Posted on 2014-09-04
3
274 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 40

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

947 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

22 Experts available now in Live!

Get 1:1 Help Now