public ActionResult Create(Employee employee) { try { var employeeDal = new Employees(); if (!ModelState.IsValid) { return(View()); } if (employeeDal.AddNewEmployee(employee)) { return(RedirectToAction("Index")); } TempData["employee"] = employee; ModelState.AddModelError("Name", "Cannot add a new employee right now, try again!"); return(View(employee)); } catch (Exception ex) { ModelState.AddModelError("Name", "Something went wrong, please try again!"); return(View(employee)); } }
private void cmdImport_Click(object sender, EventArgs e) { if (MessageBox.Show("This will remove all existing Employee Data.", "Please Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "CSV|*.csv"; var res = dlg.ShowDialog(); if (res == DialogResult.OK) { var d = dlg.FileName; var file = File.ReadAllLines(d); employees.RemoveEmployees(); foreach (string s in file.Skip(1)) { employees.AddNewEmployee(s.Split(',')[0], s.Split(',')[1], s.Split(',')[2], s.Split(',')[3]); } MessageBox.Show("Data Updated"); RefreshGrid(); } } }