Solved

Multiple items selected in a DropDownList?

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 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