private IEnumerable <module> checkAndInsertModuleConfiguration()
        {
            using (module_repository _modules = new module_repository())
            {
                IEnumerable <module> moduleConfigList = _modules.get_all();
                MODULE_CONFIG_LIST = moduleConfigList.Select(x => x.item_name).ToArray();

                if (moduleConfigList.Count() == MODULE_CONFIG_LIST.Count())
                {
                    return(moduleConfigList);
                }
                else
                {
                    foreach (string itemModuleName in MODULE_CONFIG_LIST)
                    {
                        module objExistedModuleByName = _modules.get_info_by_name(itemModuleName);
                        if (objExistedModuleByName != null)
                        {
                            continue;
                        }
                        else
                        {
                            objExistedModuleByName           = new module();
                            objExistedModuleByName.item_name = itemModuleName;
                            if (_modules.add(ref objExistedModuleByName) > 0)
                            {
                                // ok
                            }
                            else
                            {
                                // fail
                                MessageBox.Show(lang.getText("system_error"));
                                this.Close();
                                return(null);
                            }
                        }
                    }
                    moduleConfigList = _modules.get_all();
                    return(moduleConfigList);
                }
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            module_repository    modules          = new module_repository();
            IEnumerable <module> moduleConfigList = modules.get_all();

            MODULE_CONFIG_LIST = moduleConfigList.Select(x => x.item_name).ToArray();

            // saving to db
            station objStationSetting = new station_repository().get_info();

            if (isOpen(objStationSetting.socket_port) && (!txtSocketPort.Text.Equals(objStationSetting.socket_port.ToString())))
            {
                //this.btnSOCKET.Image = global::DataLogger.Properties.Resources.OFF_switch_96x25;
                //close socket
                try
                {
                    //frmNewMain.tcpListener.Stop();
                    if (Application.OpenForms.OfType <Form1>().Count() == 1)
                    {
                        Application.OpenForms.OfType <Form1>().First().Close();
                        btnShow.Enabled = false;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error ! Cant close this socket.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            try
            {
                objStationSetting.station_name = txtStationName.Text;
                objStationSetting.station_id   = txtStationID.Text;
                objStationSetting.socket_port  = Convert.ToInt32(txtSocketPort.Text.Trim());
                //MessageBox.Show("1");
                objStationSetting.module_comport = cbModule.Text;
                //MessageBox.Show("2");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cant SAVE !");
                this.Close();
                return;
            }
            try
            {
                //MessageBox.Show("3");
                if (new station_repository().update(ref objStationSetting) > 0)
                {
                    // ok
                }
                else
                {
                    // fail
                }

                foreach (string itemModule in MODULE_CONFIG_LIST)
                {
                    module obj = _modules.get_info_by_name(itemModule);
                    if (obj.type_value == 1)
                    {
                        string currentvar = obj.item_name;
                        //TextBox value = null;
                        string currentmodule     = currentvar + "Module";
                        string currentmoduleText = null;
                        GetTextBoxStrings(this, currentmodule, ref currentmoduleText);

                        string currentchannel     = currentvar + "Channel";
                        string currentchannelText = null;
                        GetTextBoxStrings(this, currentchannel, ref currentchannelText);

                        string currentinputmin     = currentvar + "InputMin";
                        string currentinputminText = null;
                        GetTextBoxStrings(this, currentinputmin, ref currentinputminText);

                        string currentinputmax     = currentvar + "InputMax";
                        string currentinputmaxText = null;
                        GetTextBoxStrings(this, currentinputmax, ref currentinputmaxText);

                        string currentoutputmin     = currentvar + "OutputMin";
                        string currentoutputminText = null;
                        GetTextBoxStrings(this, currentoutputmin, ref currentoutputminText);

                        string currentoutputmax     = currentvar + "OutputMax";
                        string currentoutputmaxText = null;
                        GetTextBoxStrings(this, currentoutputmax, ref currentoutputmaxText);

                        string currentoffset     = currentvar + "Offset";
                        string currentoffsetText = null;
                        GetTextBoxStrings(this, currentoffset, ref currentoffsetText);

                        string currentunit     = currentvar + "Unit";
                        string currentunitText = null;
                        GetTextBoxStrings(this, currentunit, ref currentunitText);

                        string currentvaluecolumn     = currentvar + "ValueColumn";
                        string currentvaluecolumnText = null;
                        GetTextBoxStrings(this, currentvaluecolumn, ref currentvaluecolumnText);

                        string currentstatuscolumn     = currentvar + "StatusColumn";
                        string currentstatuscolumnText = null;
                        GetTextBoxStrings(this, currentstatuscolumn, ref currentstatuscolumnText);

                        string currenttype     = currentvar + "Type";
                        string currenttypeText = null;
                        GetTextBoxStrings(this, currenttype, ref currenttypeText);

                        string currentdisplayname     = currentvar + "DisplayName";
                        string currentdisplaynameText = null;
                        GetTextBoxStrings(this, currentdisplayname, ref currentdisplaynameText);



                        obj.module_id      = Convert.ToInt32(currentmoduleText);
                        obj.channel_number = Convert.ToInt32(currentchannelText);
                        obj.input_min      = Convert.ToInt32(currentinputminText);
                        obj.input_max      = Convert.ToInt32(currentinputmaxText);
                        obj.output_min     = Convert.ToInt32(currentoutputminText);
                        obj.output_max     = Convert.ToInt32(currentoutputmaxText);
                        obj.off_set        = Convert.ToDouble(currentoffsetText);
                        obj.unit           = currentunitText;
                        obj.value_column   = currentvaluecolumnText;
                        obj.status_column  = currentstatuscolumnText;
                        obj.type_value     = Convert.ToInt32(currenttypeText);
                        obj.display_name   = currentdisplaynameText;

                        if (_modules.update(ref obj) > 0)
                        {
                            // ok
                        }
                        else
                        {
                            // fail
                        }
                    }
                    else if (obj.type_value == 2)
                    {
                    }
                }
                MessageBox.Show("Success", "", MessageBoxButtons.OK, MessageBoxIcon.None);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void refreshDataForControl()
        {
            // get all comport name from computer.
            string[] availableComportList;
            availableComportList = SerialPort.GetPortNames();
            module_repository    modules          = new module_repository();
            IEnumerable <module> moduleConfigList = modules.get_all();

            MODULE_CONFIG_LIST = moduleConfigList.Select(x => x.item_name).ToArray();

            if (availableComportList.Length <= 0)
            {
                MessageBox.Show(lang.getText("available_port_null"));
                //this.Close();
                //return;
            }
            // check station info setting
            station existedStationsSetting = new station_repository().get_info();

            if (existedStationsSetting == null)
            {
                existedStationsSetting = new station();
                if (new station_repository().add(ref existedStationsSetting) > 0)
                {
                    // insert ok to database
                }
                else
                {
                    MessageBox.Show(lang.getText("system_error"));
                    return;
                }
            }
            else
            {
                // set data to control from existed station setting
                txtStationName.Text = existedStationsSetting.station_name;
                txtStationID.Text   = existedStationsSetting.station_id;
                txtSocketPort.Text  = existedStationsSetting.socket_port.ToString();
                if (isOpen(existedStationsSetting.socket_port))
                {
                    this.btnSOCKET.Image = global::DataLogger.Properties.Resources.ON_switch_96x25;
                    btnShow.Enabled      = true;
                }
                else
                {
                    this.btnSOCKET.Image = global::DataLogger.Properties.Resources.OFF_switch_96x25;
                    btnShow.Enabled      = false;
                }
                var sortComportList = availableComportList.OrderBy(port => Convert.ToInt32(port.Replace("COM", string.Empty)));
                int selectedIndex   = 0;
                cbModule.Items.Clear();

                var1Module.Items.Clear();
                var2Module.Items.Clear();
                var3Module.Items.Clear();
                var4Module.Items.Clear();
                var5Module.Items.Clear();
                var6Module.Items.Clear();
                foreach (string itemAvailableComportName in sortComportList)
                {
                    cbModule.Items.Add(itemAvailableComportName);


                    if (existedStationsSetting.module_comport == itemAvailableComportName)
                    {
                        cbModule.SelectedIndex = selectedIndex;
                    }

                    selectedIndex = selectedIndex + 1;
                }
            }

            foreach (string itemModuleID in MODULE_ID_LIST)
            {
                var1Module.Items.Add(itemModuleID);
                var2Module.Items.Add(itemModuleID);
                var3Module.Items.Add(itemModuleID);
                var4Module.Items.Add(itemModuleID);
                var5Module.Items.Add(itemModuleID);
                var6Module.Items.Add(itemModuleID);
                var7Module.Items.Add(itemModuleID);
                var8Module.Items.Add(itemModuleID);
                var9Module.Items.Add(itemModuleID);
                var10Module.Items.Add(itemModuleID);
                var11Module.Items.Add(itemModuleID);
                var12Module.Items.Add(itemModuleID);
                var13Module.Items.Add(itemModuleID);
                var14Module.Items.Add(itemModuleID);
                var15Module.Items.Add(itemModuleID);
                var16Module.Items.Add(itemModuleID);
                var17Module.Items.Add(itemModuleID);
                var18Module.Items.Add(itemModuleID);
            }

            IEnumerable <module> moduleConfigurationList = checkAndInsertModuleConfiguration();

            foreach (module itemModuleSetting in moduleConfigurationList)
            {
                displayModuleSetting(itemModuleSetting);
            }
        }