예제 #1
0
파일: FormMain.cs 프로젝트: fuzzy69/Praksa5
        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string filePath = Helpers.OpenFile("Import Mesta", "CSV files (*.csv)|*.csv|All files (*.*)|*.*");

            City[] mesta = null;
            if (filePath != null)
            {
                mesta = Helpers.ReadCSVMesto(filePath);
            }
            if (mesta == null)
            {
                return;
            }
            int affectedRows = 0;

            try
            {
                foreach (City m in mesta)
                {
                    var mesto = new MESTO
                    {
                        Sifra_Mesto = m.Zip,
                        Naziv_Mesto = m.Name
                    };
                    db.MESTO.Add(mesto);
                    db.SaveChanges();
                    affectedRows++;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Update MESTO Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                // FIXME: Rollback on fail
                //dgvMestaLoad();
            }
            if (affectedRows > 0)
            {
                MessageBox.Show(String.Format("Successfully added {0} records.", affectedRows), "Update MESTO Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
예제 #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();
        }