This project is read-only.
1

Closed

Comboboxes loading from enum properties won't work

description

I'll make example of Scheduler Mode. If you look into Host > Host Settings > Other Settings > Scheduler Mode ... when you change it and update. AT next refresh it will revert back. Thats because value is feeded by enum property.
 
Its located in Host.cs
 
public static SchedulerMode SchedulerMode {
        get {
            SchedulerMode setting = DotNetNuke.Services.Scheduling.SchedulerMode.TIMER_METHOD;
            string s = GetHostSetting("SchedulerMode");
            if (!string.IsNullOrEmpty(s)) {
                setting = (Services.Scheduling.SchedulerMode)Enum.Parse(typeof(Services.Scheduling.SchedulerMode), s);
            }
            return setting;
        }
    }
 
So lets say this returns 'Disabled'.
 
Now in function which picks item from combobox on load is inside HostSettings.ascx.cs
 
if (cboSchedulerMode.Items.FindByValue(Entities.Host.Host.SchedulerMode.ToString()) != null)
        {
            cboSchedulerMode.Items.FindByValue(Entities.Host.Host.SchedulerMode.ToString()).Selected = true;
        }
        else
        {
            cboSchedulerMode.Items.FindByValue("1").Selected = true;
        }
 
Combobox here will look for value upon casting ToString on returned value which will be 'Disabled' ... Problem is Combobox doesn't know such value as it has 0,1,2 ... Thus it will always select one under "1" which is "Timer Method". So anytime you want to edit Host Settings you have to change this combobox as it will always default to Timer Method. Solution is to cast returned enum value into integer. Which will finally return 0,1 or 2
 
if (cboSchedulerMode.Items.FindByValue(((int)(Entities.Host.Host.SchedulerMode)).ToString()) != null)
        {
            cboSchedulerMode.Items.FindByValue(((int)(Entities.Host.Host.SchedulerMode)).ToString()).Selected = true;
        }
        else
        {
            cboSchedulerMode.Items.FindByValue("1").Selected = true;
        }
 
But whats really surprising is how entire file is written. Excesses of null checks look way too much paranoid. Especially when these values can't be null inside database. And I'd rather get nullexception than working with bugged code like when it can't read or parse value.
Closed Oct 7, 2010 at 2:00 PM by zyhfish

comments

Xeevis wrote Jun 13, 2010 at 4:12 AM

I submitted 2 additional patches to this issue, although seems like noone monitor patches. So take this as remainder :). I wen't through all FindByValue() in solution and these 2 are last of its kind.

wrote Oct 7, 2010 at 2:00 PM

wrote Feb 13, 2013 at 11:46 PM

wrote May 16, 2013 at 4:13 AM