Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,

in my code combobox.findstringexact is returning -1 even when the combobox has that element. Please let me know whats the problem in my code. This silly problem is wasting my precious time. I know i am missing something here. Thanks
C#
comboBoxLanguage.DataSource = Enum.GetValues(typeof(ATXDataManager.Constants.Language));
comboBoxLanguage.SelectedIndex = comboBoxLanguage.FindStringExact(objAdminData.LANGUAGE.Value.ToString());
Posted
Comments
F-ES Sitecore 18-Jun-15 9:10am    
Your problem is probably the assumption that your text contains what you're looking for, if it did you wouldn't be getting -1 back, but we can't access your dropdown or your LANGUAGE.Value data. Look for differences in case, or for spaces, leading spaces, trailing spaces etc, just look closer at your data.
[no name] 18-Jun-15 10:09am    
:( everything seems to be fine..i checked. any code i am missing to handle enums and combobox?

1 solution

If you look at the definition of FindStrignExact[^] it returns -1 when the string is not found - so this is probably expected behavior.

In this case, the problem is likely to be that the combo box has not loaded yet: you are setting the DataSource immediate before trying to search for the string so the likelyhood is that the ComboBox has not loaded the data: it doesn't do it immediately, but defers the load until it is needed.

Why search the combobox collection at all, when you could just access the collection returned by the Enum.GetValues directly instead?
 
Share this answer
 
Comments
[no name] 18-Jun-15 10:08am    
Thanks OriginalGriff for the response. The Combobox has the data loaded from the source. I checked by adding watch.
My situation is: the combobox data has to be linked to an enum. whenever i will get any value from my database, i need to show that exact value as the selected item in the combobox.
How to achieve this? :(
[no name] 18-Jun-15 10:15am    
Well I may sound stupid. But the code is working now after restarting Visual Studio :)
:D
OriginalGriff 18-Jun-15 10:24am    
Don't we all have days like that? :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900