Exemplo n.º 1
0
        private void tsmImportZaposleni_Click(object sender, EventArgs e)
        {
            string filePath = Helpers.OpenFile("Import Zaposleni", "CSV files (*.csv)|*.csv|All files (*.*)|*.*");

            object[] zaposleni = null;
            if (filePath != null)
            {
                zaposleni = Helpers.ReadCSVZaposleni(filePath);
            }
            if (zaposleni == null)
            {
                return;
            }
            int affectedRows = 0;
            var email        = new EmailAddressAttribute();

            try
            {
                foreach (Employee z in zaposleni)
                {
                    if (email.IsValid(z.Email))
                    {
                        var zaposlen = new ZAPOSLENI
                        {
                            Maticni_Broj_Zaposlenog = z.JMBG,
                            Prezime_Zaposlenog      = z.Name,
                            Ime_Zaposlenog          = z.Surname,
                            Ime_Jednog_Roditelja    = z.ParentName,
                            Adresa      = z.Address,
                            Sifra_Mesto = z.CityZip,
                            Email       = z.Email
                        };
                        db.ZAPOSLENI.Add(zaposlen);
                        db.SaveChanges();
                        affectedRows++;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Update ZAPOSLENI Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                // FIXME: Rollback on fail
                //dgvMestaLoad();
            }
            if (affectedRows > 0)
            {
                dgvPregledPodatakaRefresh();
                MessageBox.Show(String.Format("Successfully added {0} employee(s).", affectedRows), "Update ZAPOSLENI Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Exemplo n.º 2
0
        private void btnEmployeeSave_Click(object sender, EventArgs e)
        {
            if (db == null)
            {
                MessageBox.Show("Failed to reach database!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            // Validate form input
            bool ok = false;

            ok = Int64.TryParse(txtJMBG.Text, out long jmbg);
            if (!ok)
            {
                MessageBox.Show("Please enter valid JMBG!");
                return;
            }
            string employeeName = txtEmployeeName.Text.Trim();

            if (employeeName == "")
            {
                MessageBox.Show("Please enter valid employee name!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            string employeeSurname = txtEmployeeSurname.Text.Trim();

            if (employeeSurname == "")
            {
                MessageBox.Show("Please enter valid employee surname!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string employeeParentName = txtEmployeeParentName.Text.Trim();

            if (employeeParentName == "")
            {
                MessageBox.Show("Please enter valid employee parent name!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            string employeeAddress = txtEmployeeAddress.Text.Trim();

            if (employeeAddress == "")
            {
                MessageBox.Show("Please enter valid employee address!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            ok = Int32.TryParse(cbEmployeeCity.SelectedValue.ToString(), out int cityZip);
            if (!ok)
            {
                MessageBox.Show("Please select employee city!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            string employeeEmail = txtEmployeeEmail.Text.Trim();

            if (!(new EmailAddressAttribute().IsValid(employeeEmail)))
            {
                MessageBox.Show("Please enter valid employee email!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            try
            {
                // Insert employee
                if (employee == null)
                {
                    var employee_ = new ZAPOSLENI
                    {
                        Maticni_Broj_Zaposlenog = jmbg,
                        Prezime_Zaposlenog      = employeeName,
                        Ime_Zaposlenog          = employeeSurname,
                        Ime_Jednog_Roditelja    = employeeParentName,
                        Adresa      = employeeAddress,
                        Sifra_Mesto = cityZip,
                        Email       = employeeEmail
                    };
                    db.ZAPOSLENI.Add(employee_);
                    int affectedRows = db.SaveChanges();
                    if (affectedRows > 0)
                    {
                        MessageBox.Show(String.Format("Successfully added employee!", affectedRows), "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                // Update employee
                else
                {
                    var employee_ = db.ZAPOSLENI.Where(x => x.Maticni_Broj_Zaposlenog == jmbg).First();
                    employee_.Maticni_Broj_Zaposlenog = jmbg;
                    employee_.Prezime_Zaposlenog      = employeeName;
                    employee_.Ime_Zaposlenog          = employeeSurname;
                    employee_.Ime_Jednog_Roditelja    = employeeParentName;
                    employee_.Adresa      = employeeAddress;
                    employee_.Sifra_Mesto = cityZip;
                    employee_.Email       = employeeEmail;
                    int affectedRows = db.SaveChanges();
                    if (affectedRows > 0)
                    {
                        MessageBox.Show(String.Format("Successfully updated employee!", affectedRows), "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to insert/update an employee!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            frmMain mainForm = (frmMain)this.Owner;

            mainForm.dgvPregledPodatakaRefresh();
            this.Close();
        }