Solved

Multiple items selected in a DropDownList?

Posted on 2014-09-04
3
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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