private void buttonadd_Click(object sender, EventArgs e)
        {
            if (m_action == action_type.create)
            {
                #region Insert Region
                ic.departmentC _dept = new MTOMS.ic.departmentC();
                _dept.dept_name = textBox1.Text.Trim().ToProperCase();
                if (curr_node.Level == 1)
                {
                    _dept.parent_id = curr_node.Parent.Tag == null ? 0 : curr_node.Parent.Tag.ToInt32();
                }
                if (curr_node.Level == 2)
                {
                    _dept.parent_id = curr_node.Parent.Tag == null ? 0 : (curr_node.Parent.Tag as ic.departmentC).dept_id;
                }
                _dept.is_visible = true;
                _dept.level      = curr_node.Level;
                _dept.index      = curr_node.Index;
                string[] _cols = new string[]
                {
                    "exp_type",
                    "is_visible",
                    "lch_id",
                    "fs_time_stamp",
                    "dept_name",
                    "parent_id", "s_level", "s_index"
                };
                object[] _row = new object[]
                {
                    emm.export_type.insert.ToByte(),
                    1,
                         sdata.ChurchID,
                    0,
                         _dept.dept_name,
                         _dept.parent_id, _dept.level, _dept.index
                };
                using (var xd = new xing())
                {
                    if (datam.DuplicateDepartmentName(_dept.dept_name, xd))
                    {
                        MessageBox.Show("The Department Name You Have Entered Already Exists", "Duplicate Department Name");
                        buttonclose.PerformClick();
                        return;
                    }
                    _dept.dept_id  = xd.SingleInsertCommandTSPInt("dept_master_tb_ns", _cols, _row);
                    curr_node.Tag  = _dept;
                    curr_node.Text = _dept.dept_name;
                    curr_node.Name = string.Format("DEPT{0}", _dept.dept_id);
                    if (_dept.parent_id > 0)
                    {
                        _dept.expense_sys_account_id = accn.CreateChildGroupAccount(xd, datam.DATA_DEPARTMENT[_dept.parent_id].expense_sys_account_id, _dept.dept_name).account_id;
                        _dept.cr_sys_account_id      = datam.DATA_DEPARTMENT[_dept.parent_id].cr_sys_account_id;
                        _dept.income_sys_account_id  = datam.DATA_DEPARTMENT[_dept.parent_id].income_sys_account_id;
                    }
                    else
                    {
                        _dept.expense_sys_account_id = accn.CreateChildGroupAccount(xd, -2386, _dept.dept_name).account_id;
                        _dept.income_sys_account_id  = accn.CreateChildGroupAccount(xd, -2370, _dept.dept_name).account_id;
                        _dept.cr_sys_account_id      = accn.CreateChildGroupAccount(xd, -2369, _dept.dept_name).account_id;
                        xd.UpdateFsTimeStamp("accounts_tb");
                        xd.SingleUpdateCommand(string.Format("update accounts_tb set link_id={0},{1},fs_time_stamp={2} where account_id in ({3},{4},{5})", _dept.expense_sys_account_id,
                                                             dbm.ETS, SQLH.UnixStamp, _dept.expense_sys_account_id, _dept.income_sys_account_id, _dept.cr_sys_account_id));
                    }
                    //
                    xd.SingleUpdateCommandALL("dept_master_tb_ns", new string[]
                    {
                        "sys_account_id",
                        "inc_sys_account_id",
                        "cr_sys_account_id",
                        "dept_id"
                    }, new object[] { _dept.expense_sys_account_id, _dept.income_sys_account_id, _dept.cr_sys_account_id, _dept.dept_id }, 1);
                    //
                    ic.expense_accountC _exp = new ic.expense_accountC();
                    _exp.dept_id             = _dept.dept_id;
                    _exp.dept_parent_id      = _dept.parent_id;
                    _exp.dept_sys_account_id = _dept.expense_sys_account_id;
                    _exp.exp_acc_name        = string.Format("GE :: {0}", _dept.dept_name);
                    _exp.exp_acc_status      = em.exp_acc_statusS.valid;
                    _exp.exp_acc_type        = em.exp_acc_typeS.system_department;
                    datam.DATA_DEPARTMENT.Add(_dept.dept_id, _dept);
                    accn.CreateExpenseAccount(_exp, xd);
                    if (_dept.parent_id < 0)
                    {
                        #region
                        ic.accountC _inc_dept = new MTOMS.ic.accountC();
                        _inc_dept.account_name    = string.Format("GI :: {0}", _dept.dept_name);
                        _inc_dept.account_status  = em.account_statusS.Enabled;
                        _inc_dept.opening_balance = 0;
                        _inc_dept.start_date      = sdata.CURR_DATE;
                        _inc_dept.owner_type      = em.AccountOwnerTypeS.DEPARTMENT;
                        _inc_dept.owner_id        = _dept.dept_id;
                        _inc_dept.owner_name      = _dept.dept_name;
                        _inc_dept.PostType        = em.postTypeS.cash_accounts_payable;

                        ic.accountC parent_account = datam.DATA_ACCOUNTS[_dept.income_sys_account_id];
                        _inc_dept.account_dept_type     = parent_account.account_dept_type;
                        _inc_dept.account_dept_category = parent_account.account_dept_category;
                        _inc_dept.account_status        = em.account_statusS.Enabled;
                        _inc_dept.account_type          = em.account_typeS.ActualAccount;
                        _inc_dept.p_account_id          = parent_account.account_id;
                        _inc_dept.a_level = (parent_account.a_level + 1).ToInt16();
                        _inc_dept.a_index = ((datam.DATA_ACCOUNTS.Values.Count(l => l.p_account_id == parent_account.account_id & l.account_type == em.account_typeS.ActualAccount)) + 1).ToInt16();
                        _cols             = new string[]
                        {
                            "acc_d_cat_id",
                            "acc_d_type_id",
                            "account_name",
                            "account_type_id",
                            "account_alias",
                            "a_level",
                            "a_index",
                            "p_account_id",
                            "exp_type",
                            "fs_time_stamp",
                            "edate",
                            "pc_us_id",
                            "account_status_id",
                            "search_alias",
                            "is_sys_account",
                            "post_type_id",
                            "owner_type_id",
                            "owner_id",
                            "owner_name",
                            "start_date",
                            "end_date", "opening_balance",
                            "account_short_name", "account_code", "description", "lch_id", "ex_cg_type_ids", "accounts_ext_purpose"
                        };

                        xd.UpdateFsTimeStamp("accounts_tb");
                        _row = new object[]
                        {
                            _inc_dept.account_dept_category.ToByte(),
                                _inc_dept.account_dept_type.ToByte(),
                                _inc_dept.account_name,
                                _inc_dept.account_type.ToByte(),
                                _inc_dept.account_alias,
                                _inc_dept.a_level,
                                _inc_dept.a_index,
                                _inc_dept.p_account_id,
                                emm.export_type.insert.ToByte(),
                            0,
                                datam.CURR_DATE,
                                datam.PC_US_ID,
                                _inc_dept.account_status.ToByte(),
                                null,                          //search_alias
                            0,                                 // is_sys_account
                                _inc_dept.PostType.ToByte(),   //post_type
                                _inc_dept.owner_type.ToByte(), //owner_type
                                _inc_dept.owner_id,            //owner_id
                                _inc_dept.owner_name,          //owner name
                                _inc_dept.start_date,
                                _inc_dept.end_date,            //end_date
                                _inc_dept.opening_balance,     //opening balance
                                _inc_dept.account_short_name,  //account_short_name
                                _inc_dept.account_code,
                                _inc_dept.description,         //description
                                sdata.ChurchID, null, _inc_dept.extension_purpose.ToByte()
                        };
                        _inc_dept.account_id = xd.SingleInsertCommandTSPInt("accounts_tb", _cols, _row);
                        xd.UpdateFsTimeStamp("accounts_tb");
                        xd.SingleUpdateCommand(string.Format("update accounts_tb set link_id={0},{1},fs_time_stamp={2} where account_id in ({3},{4})", _dept.expense_sys_account_id,
                                                             dbm.ETS, SQLH.UnixStamp, _inc_dept.account_id, _exp.sys_account_id));

                        if (datam.DATA_ACCOUNTS != null)
                        {
                            try
                            {
                                datam.DATA_ACCOUNTS.Add(_inc_dept.account_id, _inc_dept);
                            }
                            catch (Exception)
                            {
                            }
                        }


                        #endregion
                    }
                    switch (curr_node.Level)
                    {
                    case 1:
                    {
                        curr_node.Style = _sb_dept_style;
                        break;
                    }

                    case 2:
                    {
                        curr_node.Style = _sb_dept_style2;
                        break;
                    }
                    }
                    curr_node.ContextMenu = contextMenuFile;
                    xd.CommitTransaction();
                }
                #endregion

                _dept    = null;
                m_action = action_type.none;
                if (!is_edited)
                {
                    is_edited = true;
                }
                sdata.ClearFormCache(em.fm.expense_account_settings.ToInt16());
                sdata.ClearFormCache(em.fm.chart_of_accounts.ToInt16());
                //
                sdata.ClearFormCache(em.fm.income_accounts_settings.ToInt16());
                sdata.ClearFormCache(em.fm.creditors.ToInt16());
                buttonclose.PerformClick();
            }
            if (m_action == action_type.update)
            {
                if (curr_node != null && curr_node.Tag != null)
                {
                    var _dept = curr_node.Tag as ic.departmentC;
                    if (_dept != null)
                    {
                        using (var xd = new xing())
                        {
                            if (datam.DuplicateDepartmentName(textBox1.Text.Trim().ToProperCase(), xd, _dept.dept_id))
                            {
                                MessageBox.Show("The Department Name You Have Entered Already Exists", "Duplicate Department Name");
                                buttonclose.PerformClick();
                                return;
                            }
                            xd.SingleUpdateCommandALL("dept_master_tb_ns", new string[] { "dept_name", "dept_id" }, new object[] { textBox1.Text.Trim().ToProperCase(), _dept.dept_id }, 1);
                            xd.SingleUpdateCommandALL("accounts_tb", new string[] { "account_name", "account_id" }, new object[] { textBox1.Text.Trim().ToProperCase(), _dept.expense_sys_account_id }, 1);
                            xd.CommitTransaction();
                        }
                        _dept.dept_name = textBox1.Text.Trim().ToProperCase();
                        curr_node.Text  = _dept.dept_name;
                        if (!is_edited)
                        {
                            is_edited = true;
                        }
                        sdata.ClearFormCache(em.fm.expense_account_settings.ToInt16());
                        sdata.ClearFormCache(em.fm.chart_of_accounts.ToInt16());
                        buttonclose.PerformClick();
                    }
                }
            }
        }
        public static void GetDepartments(xing _xd)
        {
            if (DATA_STAMP_STORE == null)
            {
                DATA_STAMP_STORE = new SortedList <em.StampTables, long>();
            }
            if (DATA_STAMP_STORE.IndexOfKey(em.StampTables.dept_member_tb) == -1)
            {
                DATA_STAMP_STORE.Add(em.StampTables.dept_member_tb, 0);
                DATA_STAMP_STORE.Add(em.StampTables.dept_master_tb_ns, 0);
            }
            if (DEPARTMENT_INNER_COLLECTION == null)
            {
                DEPARTMENT_INNER_COLLECTION = new Dictionary <int, string>();
                DEPARTMENT_INNER_COLLECTION.Add(-10, "Church Pastor");
                DEPARTMENT_INNER_COLLECTION.Add(-11, "Church Elders");
                DEPARTMENT_INNER_COLLECTION.Add(-12, "Family Life");
                DEPARTMENT_INNER_COLLECTION.Add(-13, "Deaconary/Deaconesses");
                DEPARTMENT_INNER_COLLECTION.Add(-14, "Treasurery");
                DEPARTMENT_INNER_COLLECTION.Add(-15, "Children Ministry");
                DEPARTMENT_INNER_COLLECTION.Add(-16, "Women Ministry");
                DEPARTMENT_INNER_COLLECTION.Add(-17, "Adventist Men");
                DEPARTMENT_INNER_COLLECTION.Add(-18, "Stewardship");
                DEPARTMENT_INNER_COLLECTION.Add(-19, "Youth");
                DEPARTMENT_INNER_COLLECTION.Add(-20, "Personal Ministry");
                DEPARTMENT_INNER_COLLECTION.Add(-21, "Education");
                DEPARTMENT_INNER_COLLECTION.Add(-22, "Health");
                DEPARTMENT_INNER_COLLECTION.Add(-23, "Communication");
                DEPARTMENT_INNER_COLLECTION.Add(-24, "Clerk/Secretary");
                DEPARTMENT_INNER_COLLECTION.Add(-25, "Interest Co-ordination");
                DEPARTMENT_INNER_COLLECTION.Add(-26, "Music");
                DEPARTMENT_INNER_COLLECTION.Add(-27, "Public Affairs and Religious Liberty");
                DEPARTMENT_INNER_COLLECTION.Add(-28, "Publishing");
                DEPARTMENT_INNER_COLLECTION.Add(-29, "Development");
                DEPARTMENT_INNER_COLLECTION.Add(-30, "Senior Citizens");
                DEPARTMENT_INNER_COLLECTION.Add(-31, "Evaluation And Monitoring");
                DEPARTMENT_INNER_COLLECTION.Add(-32, "Maintainance And Logistics");
                DEPARTMENT_INNER_COLLECTION.Add(-33, "PathFinder Ministry");
                DEPARTMENT_INNER_COLLECTION.Add(-34, "Welfare");
                DEPARTMENT_INNER_COLLECTION.Add(-35, "Administration *");
            }
            string _table_name = "dept_master_tb_ns";

            if (DATA_DEPARTMENT == null)
            {
                datam.DATA_DEPARTMENT = new SortedList <int, ic.departmentC>();
            }
            if (wdata.TABLE_STAMP == null)
            {
                wdata.TABLE_STAMP = new SortedList <string, long>();
            }
            if (wdata.TABLE_STAMP.IndexOfKey(_table_name) == -1)
            {
                wdata.TABLE_STAMP.Add(_table_name, 0);
            }
            bool is_new   = false;
            bool load_all = false;
            var  xd       = _xd == null ? new xing() : _xd;

            string _str   = string.Empty;
            var    _stamp = xd.GetTimeStamp(_table_name);

            if (DATA_DEPARTMENT.Keys.Count == 0)
            {
                _str     = "select * from dept_master_tb_ns";
                load_all = true;
            }
            else
            {
                if (wdata.TABLE_STAMP[_table_name] == _stamp)
                {
                    return;
                }
                _str = string.Format("select * from dept_master_tb_ns where fs_time_stamp > {0}", wdata.TABLE_STAMP[_table_name]);
            }
            wdata.TABLE_STAMP[_table_name] = _stamp;
            ic.departmentC _obj = null;
            #region database fill
            try
            {
                using (var _dr = xd.SelectCommand(_str))
                {
                    while (_dr.Read())
                    {
                        _obj = null;
                        if (load_all)
                        {
                            _obj   = new MTOMS.ic.departmentC();
                            is_new = true;
                        }
                        else
                        {
                            try
                            {
                                _obj   = datam.DATA_DEPARTMENT[_dr["dept_id"].ToInt32()];
                                is_new = false;
                            }
                            catch (Exception ex)
                            {
                                if (_obj == null)
                                {
                                    _obj   = new MTOMS.ic.departmentC();
                                    is_new = true;
                                }
                            }
                        }
                        if (is_new)
                        {
                            _obj.dept_id   = _dr["dept_id"].ToInt32();
                            _obj.parent_id = _dr["parent_id"].ToInt32();
                            datam.DATA_DEPARTMENT.Add(_obj.dept_id, _obj);
                        }
                        _obj.is_visible             = _dr["is_visible"].ToByte() == 0 ? false : true;
                        _obj.index                  = _dr["s_index"].ToInt16();
                        _obj.level                  = _dr["s_level"].ToInt16();
                        _obj.dept_name              = _dr["dept_name"].ToStringNullable();
                        _obj.expense_sys_account_id = _dr["sys_account_id"].ToInt32();
                        _obj.income_sys_account_id  = _dr["inc_sys_account_id"].ToInt32();
                        _obj.cr_sys_account_id      = _dr["cr_sys_account_id"].ToInt32();
                    }
                    _dr.Close(); _dr.Dispose();
                }
                #region update members
                _table_name = "dept_member_tb";
                _stamp      = xd.GetTimeStamp(_table_name);
                if (wdata.TABLE_STAMP.IndexOfKey(_table_name) == -1)
                {
                    wdata.TABLE_STAMP.Add(_table_name, 0);
                }
                if (load_all)
                {
                    _str = "select * from dept_member_tb where mem_status=1";
                }
                else
                {
                    if (wdata.TABLE_STAMP[_table_name] == _stamp)
                    {
                        return;
                    }
                    _str = string.Format("select * from dept_member_tb where fs_time_stamp>{0}", wdata.TABLE_STAMP[_table_name]);
                }
                wdata.TABLE_STAMP[_table_name] = _stamp;
                using (var _dr = xd.SelectCommand(_str))
                {
                    while (_dr.Read())
                    {
                        try
                        {
                            if (load_all)
                            {
                                datam.DATA_MEMBER[_dr["mem_id"].ToInt32()].DepartmentCollection.Add(datam.DATA_DEPARTMENT[_dr["dept_id"].ToInt32()]);
                            }
                            else
                            {
                                if (_dr["mem_status"].ToByte() == 0)
                                {
                                    if (datam.DATA_MEMBER.Keys.IndexOf(_dr["mem_id"].ToInt32()) > -1)
                                    {
                                        if (datam.DATA_MEMBER[_dr["mem_id"].ToInt32()].DepartmentCollection.IndexOf(datam.DATA_DEPARTMENT[_dr["dept_id"].ToInt32()]) > -1)
                                        {
                                            datam.DATA_MEMBER[_dr["mem_id"].ToInt32()].DepartmentCollection.Remove(datam.DATA_DEPARTMENT[_dr["dept_id"].ToInt32()]);
                                        }
                                    }
                                }
                                else
                                {
                                    if (datam.DATA_MEMBER[_dr["mem_id"].ToInt32()].DepartmentCollection.IndexOf(datam.DATA_DEPARTMENT[_dr["dept_id"].ToInt32()]) == -1)
                                    {
                                        datam.DATA_MEMBER[_dr["mem_id"].ToInt32()].DepartmentCollection.Add(datam.DATA_DEPARTMENT[_dr["dept_id"].ToInt32()]);
                                    }
                                }
                            }
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                    _dr.Close(); _dr.Dispose();
                }
                #endregion
            }
            catch (VistaDB.Diagnostic.VistaDBException ex)
            {
                MessageBox.Show(ex.Message);
                throw new Exception("Data Loading Failed");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            #endregion
            if (_xd == null)
            {
                xd.CommitTransaction();
                xd.Dispose();
                xd = null;
            }
        }