public string Save(Student aStudent) { int counter; Department department =departmentGateway.GetAll().Single(depid => depid.Id == aStudent.DepartmentId); string searchKey = department.Code + "-" + aStudent.RegDate.Year+"-"; string lastAddedRegistrationNo =GetLastAddedStudentRegistration(searchKey); if (lastAddedRegistrationNo == null) { aStudent.RegNo = searchKey + "001"; } if (lastAddedRegistrationNo != null) { string tempId = lastAddedRegistrationNo.Substring((lastAddedRegistrationNo.Length - 3), 3); counter = Convert.ToInt32(tempId); string studentSl = (counter + 1).ToString(); if (studentSl.Length==1) { aStudent.RegNo = searchKey + "00" + studentSl; } else if (studentSl.Count()==2) { aStudent.RegNo = searchKey + "0" + studentSl; } else { aStudent.RegNo = searchKey + studentSl; } } var listOfEmailAddress = from student in GetAll select student.Email; string tempEmail = listOfEmailAddress.ToList().Find(email=>email.Contains(aStudent.Email)); if (tempEmail != null) { return "Email address must be unique"; } if (IsEmailAddressValid(aStudent.Email)) { if (studentGateway.Insert(aStudent) > 0) { return "Saved Sucessfully!"; } return "Failed to save"; } return "Please! enter a valid email address"; }
public ActionResult Save(Student aStudent) { try { message = studentManager.Save(aStudent); IEnumerable<Department> departments = departmentManager.GetAll(); ViewBag.Departments = departments; ViewBag.Message =message; ViewBag.StudentInfo = aStudent; return View(); //return RedirectToAction("Index"); } catch (Exception exception) { message = exception.InnerException.Message; ViewBag.Message = message; return View(); } }
public int Insert(Student aStudent) { try { string query = "INSERT INTO t_Student VALUES(@RegNo,@Name,@Email,@ContactNo,@RegisterationDate,@Address,@DepartmentId)"; CommandObj.CommandText = query; CommandObj.Parameters.Clear(); CommandObj.Parameters.AddWithValue("@RegNo",aStudent.RegNo); CommandObj.Parameters.AddWithValue("@Name", aStudent.Name); CommandObj.Parameters.AddWithValue("@Email", aStudent.Email.ToLower()); CommandObj.Parameters.AddWithValue("@ContactNo", aStudent.Contact); CommandObj.Parameters.AddWithValue("@RegisterationDate", aStudent.RegDate.ToShortDateString()); CommandObj.Parameters.AddWithValue("@Address", aStudent.Address); CommandObj.Parameters.AddWithValue("@DepartmentId", aStudent.DepartmentId); ConnectionObj.Open(); int rowAffected = CommandObj.ExecuteNonQuery(); return rowAffected; } catch (Exception exception) { throw new Exception("Could Not save",exception); } finally { CommandObj.Dispose(); ConnectionObj.Close(); } }
public string GetLastAddedStudentRegistration(string searchKey) { string query = "SELECT * FROM t_Student st WHERE RegNo LIKE '%" + searchKey + "%' and Id=(select Max(Id) FROM t_Student st WHERE RegNo LIKE '%" + searchKey + "%' )"; CommandObj.CommandText = query; ConnectionObj.Open(); Student aStudent = null; string regNo = null; SqlDataReader reader = CommandObj.ExecuteReader(); if (reader.Read()) { aStudent = new Student { Id = Convert.ToInt32(reader["Id"].ToString()), Name = reader["Name"].ToString(), RegNo = reader["RegNo"].ToString(), Email = reader["Email"].ToString(), Contact = reader["ContactNo"].ToString(), }; regNo = aStudent.RegNo; } ConnectionObj.Close(); CommandObj.Dispose(); reader.Close(); return regNo; }