Exemple #1
0
        private void BtnSave_Click(object sender, EventArgs e)
        {
            var employeeNum = txtEmpNum.Text;
            var id          = txtIdNo.Text;
            var name        = txtFullName.Text;
            var mobile      = txtMobile.Text;
            var email       = txtEmail.Text;
            var department  = comboBoxDepartment.Text;

            Employee emp = new Employee(employeeNum, id, name, mobile, email, department);

            if (!bEdit)
            {
                var myItem = DBUtility.GetEmployees().Find(item => item.EmployeeNum == employeeNum);
                if (myItem == null)
                {
                    DBUtility.AddEmployee(emp);
                }
                else
                {
                    string message, caption;
                    string language = ChangeLanguage.ReadConfigValue("language");
                    if (language == "he-IL")
                    {
                        message = "עובד עם מספר זהה קיים במערכת";
                        caption = "שגיאה";
                    }
                    else
                    {
                        message = "Employee with this ID allready exsits in data base";
                        caption = "Error";
                    }
                    MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return;
                }
            }
            else
            {
                DBUtility.EditEmployee(emp);
            }

            //instance event args and value has been passed
            var args = new IdentityEventArgs(employeeNum, id, name, mobile, email, department);

            //Event has be raised with update arguments of delegate
            IdentityUpdated?.Invoke(this, args);

            this.Hide();
        }
        public void ImportEmployeeFromCsv()
        {
            using (var openFileDialog1 = new OpenFileDialog()
            {
                Filter = "CSV|*.csv", ValidateNames = true, Multiselect = false
            })
            {
                if (openFileDialog1.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                const char sepChar      = ',';
                const char quoteChar    = '"';
                var        employeeList = new List <string[]>();
                try
                {
                    using (Stream stream = null)
                    {
                        var rows = File.ReadAllLines(openFileDialog1.FileName);
                        foreach (var csvRow in rows)
                        {
                            var inQuotes = false;
                            var fields   = new List <string>();
                            var field    = "";
                            for (var i = 0; i < csvRow.Length; i++)
                            {
                                if (inQuotes)
                                {
                                    if (i < csvRow.Length - 1 && csvRow[i] == quoteChar && csvRow[i + 1] == quoteChar)
                                    {
                                        i      = i++;
                                        field += quoteChar;
                                    }
                                    else if (csvRow[i] == quoteChar)
                                    {
                                        inQuotes = false;
                                    }
                                    else
                                    {
                                        if (csvRow[i - 1] == quoteChar)
                                        {
                                            field  = "";
                                            field += csvRow[i];
                                        }
                                        else
                                        {
                                            field += csvRow[i];
                                        }
                                    }
                                }
                                else
                                {
                                    if (csvRow[i] == quoteChar)
                                    {
                                        inQuotes = true;
                                    }
                                    if (csvRow[i] == sepChar)
                                    {
                                        fields.Add(field);
                                        field = "";
                                    }
                                    else
                                    {
                                        field += csvRow[i];
                                    }
                                }
                            }
                            if (!string.IsNullOrEmpty(field))
                            {
                                fields.Add(field);
                                field = "";
                            }
                            employeeList.Add(fields.ToArray());
                        }
                    }
                }
                catch (Exception er)
                {
                    MessageBox.Show(er.Message, "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                foreach (var value in employeeList)
                {
                    {
                        dataGridView.Rows.Add(value[0], value[1], value[2], value[3], value[4], value[5]);

                        try
                        {
                            Employee emp = new Employee(value[0], value[1], value[2], value[3], value[4], value[5]);
                            DBUtility.AddEmployee(emp);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
        }