/// <summary>
        /// Get selected record ID.
        /// </summary>
        public retStatus GetRecordId(string table_name, int column, string value, out int id)
        {
            id = -1;
            string dbValue;

            retStatus status = GetDataTable(table_name, out DataTable dataTable);

            if (status.IsError)
            {
                return(status);
            }

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                dbValue = Regex.Replace(dataTable.Rows[i][1].ToString(), @"\s+", "");
                if (dbValue.Equals(value))
                {
                    if (!int.TryParse(dataTable.Rows[i][0].ToString(), out id))
                    {
                        return(new retStatus(retStatus.ReturnCodes.ERR_PARSING_DATA));
                    }
                    return(status);
                }
            }

            return(new retStatus(retStatus.ReturnCodes.WARNING_SQL_UNABLE_TO_FIND_RECORD_ID));
        }
Beispiel #2
0
        public retStatus GetElements(string parentName, out Element[] elements)
        {
            elements = new Element[0];
            if (xmlElement == null)
            {
                return(new retStatus(retStatus.ReturnCodes.ERR_XML_NOT_LOADED));
            }

            retStatus status = GetXElement(parentName, out XElement parent);

            if (status.IsError)
            {
                return(status);
            }

            List <Element> retElements = new List <Element>();

            foreach (XElement element in parent.Elements())
            {
                retElements.Add(new Element {
                    Name = element.Name.LocalName, Value = element.Value
                });
            }

            elements = retElements.ToArray();

            return(status);
        }
Beispiel #3
0
        public retStatus GetAttributes(string parentName, out Attribute[] attributes)
        {
            attributes = new Attribute[0];
            if (xmlElement == null)
            {
                return(new retStatus(retStatus.ReturnCodes.ERR_XML_NOT_LOADED));
            }

            retStatus status = GetXElement(parentName, out XElement parent);

            if (status.IsError)
            {
                return(status);
            }

            List <Attribute> retAttributes = new List <Attribute>();

            foreach (XAttribute attribute in parent.Attributes())
            {
                retAttributes.Add(new Attribute {
                    Name = attribute.Name.LocalName, Value = attribute.Value
                });
            }

            attributes = retAttributes.ToArray();
            return(status);
        }
Beispiel #4
0
        /// <summary>
        /// Create elements for selcted parent
        /// </summary>
        /// <param name="parentName">Parent name</param>
        /// <param name="elements">Array of elements to be created</param>
        /// <returns></returns>
        public retStatus AddElements(string parentName, Element[] elements)
        {
            if (xmlElement == null)
            {
                return(new retStatus(retStatus.ReturnCodes.ERR_XML_NOT_LOADED));
            }
            try
            {
                retStatus status = GetXElement(parentName, out XElement parent);
                if (status.IsError)
                {
                    return(status);
                }

                // Add all elements
                foreach (Element element in elements)
                {
                    parent.Add(new XElement(element.Name, element.Value));
                }


                xmlElement.Save(filePath);
                return(new retStatus());
            }
            catch
            {
                return(new retStatus(retStatus.ReturnCodes.ERR_XML_BAD_ELEMENT));
            }
        }
Beispiel #5
0
        public retStatus GetElementValue(string parentName, string elementName, out Element retElement)
        {
            retElement = new Element();
            if (xmlElement == null)
            {
                return(new retStatus(retStatus.ReturnCodes.ERR_XML_NOT_LOADED));
            }

            retStatus status = GetXElement(parentName, out XElement parent);

            if (status.IsError)
            {
                return(status);
            }

            foreach (XElement element in parent.Elements())
            {
                if (element.Name == elementName)
                {
                    retElement = new Element {
                        Name = elementName, Value = element.Value
                    };
                    return(new retStatus());
                }
            }

            return(new retStatus(retStatus.ReturnCodes.ERR_XML_XELEMENT_MISSING));
        }
        /// <summary>
        /// Load all configuraiton information from SQL database for selected model number
        /// </summary>
        /// <returns>Load status</returns>
        private retStatus LoadModelConfig()
        {
            DataRow[] selectedConfig;

            // Check if connection to SQL server is available
            if (!isDatabaseAvialable || !sqlServer.IsConnected)
            {
                return(new retStatus(retStatus.ReturnCodes.ERR_SQL_DATABASE_CONNECTION));
            }

            // Check if model number is selected. Less than zero index is set when nothing is available
            if (cbModel.SelectedIndex < 0)
            {
                return(new retStatus(retStatus.ReturnCodes.ERR_SQL_MODEL_NOT_SELECTED));
            }

            // Get ID from sql server of selected model number
            retStatus status = sqlServer.GetRecordId(dbtModel, 1, cbModel.Text, out int id);

            if (status.RetCode != 0)
            {
                return(status);                     // Warnings are possible
            }
            // Pull configuration table data from sql server for selcted ID number
            status = sqlServer.GetDataTableWithID(dbtConfig, dbColModelID, id, out DataTable dataTable);
            if (status.IsError)
            {
                return(status);
            }

            // Store configuration data that is matching model ID
            selectedConfig = dataTable.Select($"{dbColModelID} = {id}");

            // Clear configuration controls
            ClearTextBoxText(pnlMotorConfig);
            ClearComboBox(pnlMotorConfig);

            // Go trough all configuraiton values and store them inside configruation data structure
            for (int i = 0; i < selectedConfig.Length; i++)
            {
                // Get configuration name at current index
                string configName = selectedConfig[i][1].ToString().Trim();

                // Check if configuraiton name from sql server exist inside configuration data structure
                if (!mcConfiguraiton.ContainsKey(configName))
                {
                    continue;
                }

                mcConfiguraiton[configName].UpdateData(selectedConfig[i][2].ToString().Trim());
                mcConfiguraiton[configName].SetData(); // Update UI
            }
            return(status);
        }
Beispiel #7
0
        public retStatus ClearParentNodes(string parentName)
        {
            retStatus status = GetXElement(parentName, out XElement parent);

            if (status.IsError)
            {
                return(status);
            }

            parent.RemoveNodes();
            xmlElement.Save(filePath);

            return(status);
        }
        private void cbModel_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Load selected model number configuraiton
            retStatus status = LoadModelConfig();

            if (status.RetCode != 0)
            {
                MessageBox.Show("Unable to load selected model configuraiton from database");
            }
            else
            {
                MessageBox.Show($"Loaded configuraiton for model: {cbModel.Text}");
            }
        }
        private void btnMCUpdate_Click(object sender, EventArgs e)
        {
            // Check if connection to SQL server is available
            if (!isDatabaseAvialable || !sqlServer.IsConnected)
            {
                return;
            }

            retStatus status = sqlServer.GetRecordId(dbtModel, 1, cbModel.Text, out int id);

            if (status.RetCode == retStatus.ReturnCodes.WARNING_SQL_UNABLE_TO_FIND_RECORD_ID)
            {
                MessageBox.Show($"Selected model number is not inside database.");
                return;
            }
            else if (status.IsError)
            {
                return;
            }

            List <string> colName = new List <string> {
                dbColConfigValue
            };
            List <string> colValue;

            // Go through all configurations inside dictionary
            foreach (string configName in mcConfiguraiton.Keys)
            {
                // Check if configuraiton should be saved inside database
                if (!mcConfiguraiton[configName].SaveInsideDB)
                {
                    continue;
                }

                // Update record
                status = sqlServer.UpdateRecord(dbtConfig, colName, new List <string> {
                    mcConfiguraiton[configName].GetStringData()
                }, dbColModelID, id, dbColConfigName, configName);
                if (status.IsError)
                {
                    MessageBox.Show($"Unable to update values inside database. Error: {status.RetCode}");
                    return;
                }
            }

            MessageBox.Show($"Updated configuraiton for model: {cbModel.Text} inside database");
        }
        private void btnMCRemove_Click(object sender, EventArgs e)
        {
            // Check if connection to SQL server is available
            if (!isDatabaseAvialable || !sqlServer.IsConnected)
            {
                return;
            }

            DialogResult result = MessageBox.Show($"Remove {cbModel.Text} From Database?", "Remove Model", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

            if (result == DialogResult.No)
            {
                return;
            }

            retStatus status = sqlServer.GetRecordId(dbtModel, 1, cbModel.Text, out int id);

            if (status.RetCode == retStatus.ReturnCodes.WARNING_SQL_UNABLE_TO_FIND_RECORD_ID)
            {
                MessageBox.Show($"Selected model number is not inside database.");
                return;
            }
            else if (status.IsError)
            {
                return;
            }

            status = sqlServer.DeleteRecord(dbtConfig, dbColModelID, id);
            if (status.IsError)
            {
                MessageBox.Show($"Unable remove record from database. Error: {status.RetCode}");
                return;
            }

            status = sqlServer.DeleteRecord(dbtModel, dbColModelID, id);
            if (status.IsError)
            {
                MessageBox.Show($"Unable remove record from database. Error: {status.RetCode}");
                return;
            }

            MessageBox.Show($"Removed model: {cbModel.Text} from database");

            cbModel.Items.RemoveAt(cbModel.Items.IndexOf(cbModel.Text));
            ClearTextBoxText(pnlMotorConfig);
            ClearComboBox(pnlMotorConfig);
        }
        public bool IdExists(string table_name, int id)
        {
            string dbValue;

            retStatus status = GetDataTable(table_name, out DataTable dataTable);

            if (status.IsError)
            {
                return(false);
            }

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                dbValue = Regex.Replace(dataTable.Rows[i][0].ToString(), @"\s+", "");
                if (dbValue.Equals(id.ToString()))
                {
                    return(true);
                }
            }
            return(false);
        }
        /// <summary>
        /// Get list of all available model numbers and place them inside combo box
        /// </summary>
        /// <returns>Load model list status</returns>
        private retStatus GetModelList()
        {
            // Check if connection to SQL server is available
            if (!isDatabaseAvialable || !sqlServer.IsConnected)
            {
                return(new retStatus(retStatus.ReturnCodes.ERR_SQL_DATABASE_CONNECTION));
            }

            // Clear combo box with model numbers
            cbModel.Items.Clear();

            // Get table from sql server from model table
            retStatus status = sqlServer.GetDataTable(dbtModel, out DataTable dataTable);

            if (status.IsError)
            {
                return(status);
            }

            // Add all model numbers inside combo box
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                // Go trough each row of the table and only input mode numbers for selected device
                if (dataTable.Rows[i][2].ToString().Equals(deviceMC))
                {
                    cbModel.Items.Add(dataTable.Rows[i][1].ToString().Trim()); // Remove any spaces from number
                }
            }

            // Check if any model numbers avaialbe
            if (cbModel.Items.Count > 0)
            {
                // Select first model number inside list
                cbModel.SelectedIndex = 0;
                LoadModelConfig();
            }

            return(new retStatus());
        }
        /// <summary>
        /// Get last ID inside selected table.
        /// </summary>
        /// <param name="table_name">Selected table name.</param>
        /// <param name="Id">Selected record ID.</param>
        /// <returns>SQL Query status</returns>
        public retStatus GetLastId(string table_name, out int Id)
        {
            Id = -1;

            retStatus status = GetDataTable(table_name, out DataTable dataTable);

            if (status.IsError)
            {
                return(status);
            }

            if (dataTable.Rows.Count != 0)
            {
                Id = (int)dataTable.Rows[dataTable.Rows.Count - 1][0];
            }
            else
            {
                return(new retStatus(retStatus.ReturnCodes.ERR_SQL_BAD_QUERY));
            }

            return(status);
        }
Beispiel #14
0
 /// <summary>
 /// Create attribute for selected parent
 /// </summary>
 /// <param name="parentName">Parent element name</param>
 /// <param name="attributeName">Attribute name</param>
 /// <param name="attributeValue">Attribute value</param>
 /// <returns></returns>
 public retStatus AddAttribute(string parentName, Attribute attribute)
 {
     if (xmlElement == null)
     {
         return(new retStatus(retStatus.ReturnCodes.ERR_XML_NOT_LOADED));
     }
     try
     {
         retStatus status = GetXElement(parentName, out XElement parent);
         if (status.IsError)
         {
             return(status);
         }
         parent.Add(new XAttribute(attribute.Name, attribute.Value));
         xmlElement.Save(filePath);
         return(status);
     }
     catch
     {
         return(new retStatus(retStatus.ReturnCodes.ERR_XML_BAD_ATTRIBUTE));
     }
 }
        private void btnMCAddNew_Click(object sender, EventArgs e)
        {
            retStatus status = new retStatus();

            // Check if connection to SQL server is available
            if (!isDatabaseAvialable || !sqlServer.IsConnected)
            {
                return;
            }

            // make sure that current model does not exist already inside database
            status = sqlServer.GetRecordId(dbtModel, 1, cbModel.Text, out int id);
            if (status.RetCode == 0)
            {
                return;                      // Already exist
            }
            // create new model
            List <string> colName = new List <string> {
                dbColModelNumber, dbColDeviceName
            };
            List <string> colValue = new List <string> {
                cbModel.Text, deviceMC
            };

            status = sqlServer.AddNewRecord(dbtModel, colName, colValue);
            if (status.IsError)
            {
                MessageBox.Show($"Unable to add new record inside database. Error: {status.RetCode}");
                return;
            }

            status = sqlServer.GetLastId(dbtModel, out id);
            if (status.IsError)
            {
                MessageBox.Show($"Unable to get newly created ID from database. Error: {status.RetCode}");
                return;
            }

            // set configuraiton table column names
            colName = new List <string> {
                dbColModelID, dbColConfigName, dbColConfigValue
            };

            // Go through all configurations inside dictionary
            foreach (string configName in mcConfiguraiton.Keys)
            {
                // Check if configuraiton should be saved inside database
                if (!mcConfiguraiton[configName].SaveInsideDB)
                {
                    continue;
                }

                // Store config name
                colValue = new List <string> {
                    id.ToString(), configName, mcConfiguraiton[configName].GetStringData()
                };
                status = sqlServer.AddNewRecord(dbtConfig, colName, colValue);
                if (status.IsError)
                {
                    MessageBox.Show($"Unable to add new record inside database. Error: {status.RetCode}");
                    return;
                }
            }
            MessageBox.Show($"Added new configuraiton for model: {cbModel.Text} inside database");
        }
Beispiel #16
0
 public myXML(string _fileName, Environment.SpecialFolder folder, out retStatus status)
 {
     filePath = Environment.GetFolderPath(folder) + $"\\{_fileName}";
     status   = LoadFile();
 }
Beispiel #17
0
 public myXML(string _filePath, out retStatus status)
 {
     filePath = _filePath;
     status   = LoadFile();
 }