I can't figure this out. Sometimes it throws the exception sometimes it works. Right here is the situation, i have a custom property editorpart that i wrote, it edits a custom webpart i worte.
The editor part gets a list of images from a directory then displays them in a drop down list, it then gets a property from the webpart and depending on what is asked will get or set the image in that web part based on the value in the ddlist item (as you'd expect). There is also a file upload that uploads an image to the directory the ddlist gets it's items from. This works fine and has been working great, until i decided to sort the items in the list.
i worte a method that is to sort the items in the list and when i call it sometimes it works but other times it will throw an exception:
Item has already been added. Key in dictionary: 'image.jpg' Key being added: 'image.jpg'
i tried debugging and when it does throw the exception it is going around in the loop again and tries to add it to the SortedList again, here is my code.
Private Sub SortDropDownList(ByVal dd As DropDownList)
'This might cause issues.
If Not (dd Is Nothing) Then
Dim sl As New SortedList
Dim selected As String = dd.SelectedValue
For Each i As ListItem In dd.Items
sl.Add(i.Text, i)
Next
dd.Items.Clear()
Dim o As DictionaryEntry
For Each o In sl
dd.Items.Add(o.Value)
Next
dd.SelectedValue = selected
sl.Clear()
End If
End Sub
Any ideas?
Micky B
Intranet Developer