protected void RefreshCommandsView()
    {
        Hashtable State = (Hashtable)HttpRuntime.Cache[Session.SessionID];
        ArrayList DBCommands = (ArrayList)State["DBCommands"];
        RadTreeNode DatabaseCommandRoot = CreateSpreadsheetRootNode();

        if (DBCommands == null)
            return;

        //re add the previous controls
        int command_index = 0;
        Hashtable prev_CommandEntry = null;
        foreach (Hashtable CommandEntry in DBCommands)
        {
            int sub_command_index = 0;
            RadTreeNode command_node = new RadTreeNode();
            command_node.CssClass = "RadTreeView";
            command_node.Category = "command";
            command_node.PostBack = false;

            string orig_command = CommandEntry["command"].ToString();
            string command = orig_command.ToLower();
            State["SelectedSqlCommand"] = command;

            Control CommandControl = null;
            string selected_table = null;
            if (CommandEntry["table"] != null && CommandEntry["table"].ToString().Length > 0)
            {
                State["SelectedDatabaseTable"] = selected_table = CommandEntry["table"].ToString();
                DataSources DS = new DataSources();
                State["DataSourceDatabaseTableFields"] = DS.GetDataSourceDatabaseTableFields(State);
                if (State["DataSourceDatabaseTableFields"] == null)
                {
                    ResetDatabaseConfig_Click(null, null);
                    Message.Text = "Internal Error:  All commands have been cleared.";
                    State["SpreadsheetError"] = null;
                    State["DBCommands"] = null;
                    State["SelectedDatabaseTable"] = null;
                    State["SelectedSqlCommand"] = null;
                    return ;
                }
            }

            if (command == "if")
            {
                CommandControl = LoadControl("Controls/IfDeviceFieldThenDoCommand.ascx");
            }
            else if(command.EndsWith("go to page"))
            {
                string selected_page = null;
                if (CommandEntry["page"] != null && CommandEntry["page"].ToString().Length > 0)
                {
                    State["SelectedGoToPage"] = selected_page = CommandEntry["page"].ToString();
                }
                CommandControl = LoadControl("Controls/GoToPage.ascx", selected_page);
                if (command.StartsWith("then "))
                {
                    command = command.Remove(0, 5);
                    ((RadComboBox)CommandControl.FindControl("command")).SelectedValue = command;
                    ((Label)CommandControl.FindControl("command_prefix")).Text = "Then ";
                }
                else if (command.StartsWith("else "))
                {
                    command = command.Remove(0, 5);
                    ((RadComboBox)CommandControl.FindControl("command")).SelectedValue = command;
                    ((Label)CommandControl.FindControl("command_prefix")).Text = "Else ";
                }
                else
                    ((RadComboBox)CommandControl.FindControl("command")).SelectedValue = command;

            }
            else
            {
                CommandControl = LoadControl("Controls/DatabaseCommand.ascx", selected_table);
                if (command.StartsWith("then "))
                {
                    command = command.Remove(0, 5);
                    ((RadComboBox)CommandControl.FindControl("command")).SelectedValue = command;
                    ((Label)CommandControl.FindControl("command_prefix")).Text = "Then ";
                }
                else if (command.StartsWith("else "))
                {
                    command = command.Remove(0, 5);
                    ((RadComboBox)CommandControl.FindControl("command")).SelectedValue = command;
                    ((Label)CommandControl.FindControl("command_prefix")).Text = "Else ";
                }
                else
                    ((RadComboBox)CommandControl.FindControl("command")).SelectedValue = command;

            }
            prev_CommandEntry = CommandEntry;

            command_node.Controls.Add(CommandControl);
            DatabaseCommandRoot.Nodes.Add(command_node);

            string field_control_type = null;

            if (command == "if")
            {
                if (CommandEntry["command_condition_device_field1"] != null && CommandEntry["command_condition_device_field1"].ToString().Length > 0)
                {
                    string command_condition_device_field1 = CommandEntry["command_condition_device_field1"].ToString();
                    ((HtmlInputText)CommandControl.FindControl("command_condition_device_field1")).Value = command_condition_device_field1;
                }
                else
                {
                    CommandEntry["command_condition_device_field1"] = ((HtmlInputText)CommandControl.FindControl("command_condition_device_field1")).Value;
                }
                if (CommandEntry["command_condition_operation"] != null && CommandEntry["command_condition_operation"].ToString().Length > 0)
                {
                    string command_condition_operation = CommandEntry["command_condition_operation"].ToString();
                    ((RadComboBox)CommandControl.FindControl("command_condition_operation")).SelectedValue = command_condition_operation;
                }
                else
                {
                    CommandEntry["command_condition_operation"] = ((RadComboBox)CommandControl.FindControl("command_condition_operation")).SelectedValue;
                }
                if (CommandEntry["command_condition_device_field2"] != null && CommandEntry["command_condition_device_field2"].ToString().Length > 0)
                {
                    string command_condition_device_field2 = CommandEntry["command_condition_device_field2"].ToString();
                    HtmlInputText command_condition_device_field2_input = ((HtmlInputText)CommandControl.FindControl("command_condition_device_field2"));
                    command_condition_device_field2_input.Value = command_condition_device_field2;
                }
                else
                {
                    CommandEntry["command_condition_device_field2"] = ((HtmlInputText)CommandControl.FindControl("command_condition_device_field2")).Value;
                }
                continue;
            }

            else if (command == "insert into")
            {
                ((ImageButton)CommandControl.FindControl("add_condition")).Visible = false;
                ((ImageButton)CommandControl.FindControl("add_order_by")).Visible = false;
                field_control_type = "from_phone_to_database";
            }
            else if (command == "update")
            {
                ((ImageButton)CommandControl.FindControl("add_order_by")).Visible = false;
                field_control_type = "from_phone_to_database";
            }
            else if (command == "select from")
            {
                field_control_type = "from_database_to_phone";
            }
            else if (command == "delete from")
            {
                ((ImageButton)CommandControl.FindControl("add_field")).Visible = false;
                ((ImageButton)CommandControl.FindControl("add_order_by")).Visible = false;
            }

             ArrayList DBFields = (ArrayList)CommandEntry["database_fields"];
            if (DBFields != null && field_control_type != null)
            {
                foreach (Hashtable FieldEntry in DBFields)
                {
                    RadTreeNode field_node = new RadTreeNode();
                    field_node.CssClass = "RadTreeView";
                    field_node.Category = "field";
                    field_node.PostBack = false;
                    State["SelectedDatabaseTable"] = selected_table;
                    string control_file = (field_control_type == "from_database_to_phone") ? "DatabaseToDeviceField.ascx" : "DeviceToDatabaseField.ascx";
                    string selected_database_field = null;
                    if (FieldEntry["database_field"] != null && FieldEntry["database_field"].ToString().Length > 0)
                    {
                        selected_database_field = FieldEntry["database_field"].ToString();
                    }
                    else
                    {
                        ArrayList DataSourceDatabaseTableFields = (ArrayList)State["DataSourceDatabaseTableFields"];
                        selected_database_field = DataSourceDatabaseTableFields[0].ToString();
                    }

                    Control FieldControl = LoadControl("Controls/" + control_file, selected_database_field);
                    if (State["SpreadsheetError"] != null)
                    {
                        ResetDatabaseConfig_Click(null, null);
                        Message.Text = State["SpreadsheetError"].ToString();
                        State["SpreadsheetError"] = null;
                        State["DBCommands"] = null;
                        State["SelectedDatabaseTable"] = null;
                        State["SelectedSqlCommand"] = null;
                        return;
                    }
                    field_node.Controls.Add(FieldControl);
                    command_node.Nodes.Add(field_node);

                    if (FieldEntry["device_field"] != null && FieldEntry["device_field"].ToString().Length > 0)
                    {
                        string phone_field = FieldEntry["device_field"].ToString();
                        HtmlInputText phone_field_input = (HtmlInputText)FieldControl.FindControl("device_field");
                        phone_field_input.Value = phone_field;
                    }
                    sub_command_index++;
                }
            }
            ArrayList DBWhere = (ArrayList)CommandEntry["conditions"];
            if (DBWhere != null)
            {
                int i_condition = 0;
                // Hashtable uniqueConditionIDs = new Hashtable();
                foreach (Hashtable ConditionEntry in DBWhere)
                {
                    if (i_condition == 0)
                        State["IsFirstCommandCondition"] = true;
                    else
                        State["IsFirstCommandCondition"] = false;

                    RadTreeNode where_node = new RadTreeNode();
                    where_node.CssClass = "RadTreeView";
                    where_node.Category = "condition";
                    where_node.PostBack = false;
                    State["SelectedDatabaseTable"] = selected_table;
                    string selected_condition_1st_field = null;
                    if (ConditionEntry["condition_1st_field"] != null && ConditionEntry["condition_1st_field"].ToString().Length > 0)
                    {
                        selected_condition_1st_field = ConditionEntry["condition_1st_field"].ToString();
                    }
                    else
                    {
                        ArrayList DataSourceDatabaseTableFields = (ArrayList)State["DataSourceDatabaseTableFields"];
                        selected_condition_1st_field = DataSourceDatabaseTableFields[0].ToString();
                    }
                    Control WhereControl = LoadControl("Controls/SpreadsheetWhere.ascx", selected_condition_1st_field);

                    if (State["SpreadsheetError"] != null)
                    {
                        ResetDatabaseConfig_Click(null, null);
                        Message.Text = State["SpreadsheetError"].ToString();
                        State["SpreadsheetError"] = null;
                        State["DBCommands"] = null;
                        State["SelectedDatabaseTable"] = null;
                        State["SelectedSqlCommand"] = null;
                        return;
                    }

                    where_node.Controls.Add(WhereControl);

                    command_node.Nodes.Add(where_node);

                    if (ConditionEntry["condition_operation"] != null && ConditionEntry["condition_operation"].ToString().Length > 0)
                    {
                        string condition_operation = ConditionEntry["condition_operation"].ToString();
                        ((RadComboBox)WhereControl.FindControl("condition_operation")).SelectedValue = condition_operation;
                    }
                    else
                    {
                        ConditionEntry["condition_operation"] = ((RadComboBox)WhereControl.FindControl("condition_operation")).SelectedValue;
                    }
                   if (ConditionEntry["field_operation"] != null && ConditionEntry["field_operation"].ToString().Length > 0)
                    {
                        string field_operation = ConditionEntry["field_operation"].ToString();
                        ((RadComboBox)WhereControl.FindControl("field_operation")).SelectedValue = field_operation;
                    }
                    else
                    {
                        ConditionEntry["field_operation"] = ((RadComboBox)WhereControl.FindControl("field_operation")).SelectedValue;
                    }
                    if (ConditionEntry["condition_2nd_field"] != null && ConditionEntry["condition_2nd_field"].ToString().Length > 0)
                    {
                        string condition_2nd_field = ConditionEntry["condition_2nd_field"].ToString();
                        HtmlInputText condition_2nd_field_input = (HtmlInputText)WhereControl.FindControl("condition_2nd_field");
                        condition_2nd_field_input.Value = condition_2nd_field;
                    }
                    i_condition++;
                    sub_command_index++;
                }
            }
            Hashtable DBOrderBy = (Hashtable)CommandEntry["order_by"];
            if (DBOrderBy != null)
            {
                RadTreeNode orderBy_node = new RadTreeNode();
                orderBy_node.CssClass = "RadTreeView";
                orderBy_node.Category = "order_by";
                orderBy_node.PostBack = false;
                State["SelectedDatabaseTable"] = selected_table;
                string selected_sort_field = null;
                if (DBOrderBy["sort_field"] != null && DBOrderBy["sort_field"].ToString().Length > 0)
                {
                    selected_sort_field = DBOrderBy["sort_field"].ToString();
                }
                else
                {
                    ArrayList DataSourceDatabaseTableFields = (ArrayList)State["DataSourceDatabaseTableFields"];
                    selected_sort_field = DataSourceDatabaseTableFields[0].ToString();
                }

                Control OrderByControl = LoadControl("Controls/DatabaseOrderBy.ascx", selected_sort_field);
                if (State["SpreadsheetError"] != null)
                {
                    ResetDatabaseConfig_Click(null, null);
                    Message.Text = State["SpreadsheetError"].ToString();
                    State["SpreadsheetError"] = null;
                    State["DBCommands"] = null;
                    State["SelectedDatabaseTable"] = null;
                    State["SelectedSqlCommand"] = null;
                    return;
                }

                orderBy_node.Controls.Add(OrderByControl);
                command_node.Nodes.Add(orderBy_node);

                if (DBOrderBy["sort_direction"] != null && DBOrderBy["sort_direction"].ToString().Length > 0)
                {
                    string sort_direction = DBOrderBy["sort_direction"].ToString();
                    ((RadComboBox)OrderByControl.FindControl("sort_direction")).SelectedValue = sort_direction;
                }
                sub_command_index++;
            }

            command_node.ExpandChildNodes();
            command_index++;
        }
        SpreadsheetCommandsView.ExpandAllNodes();
    }