Solved

Multiple items selected in a DropDownList?

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

696 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