Search code examples
c#duplicatessortedlist

Item duplication problem


My goal is to add a insert new value to a column where my column values are as follows

100 * 100
150 * 150
200 * 200
200 * 200

I get the following error:

Item has already been added. Key in dictionary: '200 x 200' Key being added: '200 x 200'

For next code:

SortedList sortedList = new SortedList();

foreach (ListItem listItem in ddldimension.Items)
    sortedList.Add(listItem.Text, listItem.Value);

if (!sortedList.ContainsKey(CommonUtilities.GetCustomString("DefaultValues", "defaultEmbedDimension1")))
    sortedList.Add(CommonUtilities.GetCustomString("DefaultValues", "defaultEmbedDimension1"), "defaultEmbedDimension1");

if (!sortedList.ContainsKey(CommonUtilities.GetCustomString("DefaultValues", "defaultEmbedDimension2")))
    sortedList.Add(CommonUtilities.GetCustomString("DefaultValues", "defaultEmbedDimension2"), "defaultEmbedDimension2");

if (!sortedList.ContainsKey(CommonUtilities.GetCustomString("DefaultValues", "defaultEmbedDimension3")))
    sortedList.Add(CommonUtilities.GetCustomString("DefaultValues", "defaultEmbedDimension3"), "defaultEmbedDimension3");

Solution

  • From the error message you're getting, and from the documentation for SortedList:

    In either case, a SortedList does not allow duplicate keys.

    So it would appear that a SortedList isn't the right structure for you to be using in your application. Unfortunately, you've provided insufficient information to allow me to suggest something better.