Question

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");
Was it helpful?

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.

OTHER TIPS

SortedList does not allow adding duplicate keys. Use List<> (along with KeyValuePair for example) instead (eg. List<KeyValuePair<string, object>>).

Here is the solution for your code:

var list = new List<KeyValuePair<string, string>>();

foreach (var item in ddldimension.Items)
{
 list.Add(new KeyValuePair<string, string>(item.Text, item.Value));
}

var defaultEmbedDimension1 = CommonUtilities.GetCustomString("DefaultValues", "defaultEmbedDimension1");
int index = list.FindIndex(k => k.Key == defaultEmbedDimension1); // If there is no such Key, it will be -1. If you want to find by Value, replace k.Key by k.Value
if (index >= 0)
{
 list.Add(new KeyValuePair<string, string>(defaultEmbedDimension1, "defaultEmbedDimension1"));
}

In this way, you allow to keep duplicate keys in your structure. Note you invoke the same method twice. Initialize variable instead:

string defaultEmbedDimension1 = CommonUtilities.GetCustomString("DefaultValues", "defaultEmbedDimension1");

To populate list, you can alternatively use LINQ:

var list = ddldimensions.Items.Select(item => new KeyValuePair<string, string>(item.Text, item.Value)).ToList();

Read also: C# KeyValuePair Collection Hints at Dot Net Perls.

But if you decide to disallow duplicates and gently deal with them in SortedList, you can create an extension:

public static class SortedListExtensions
{
 public static bool AddIfNotContains<K, V>(this IDictionary<K, V> dictionary, K key, V value)
 {
  if (!dictionary.ContainsKey(key))
  {
   dictionary.Add(key, value);
   return true;
  }
  return false;
 }
}

And use it as I did below, without throwing exception:

var sortedList = new SortedList<string, string>();
sortedList.Add("a", "b");
sortedList.AddIfNotContains("a", "b"); // Will not be added
sortedList.AddIfNotContains("b", "b"); // Will be added
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top