public ActionResult RegisterLec(string action, RegisterLec f) { if (!ModelState.IsValid) { return(View()); } else { if (f.Lec_Pass != f.Lec_Pass_Confirm) { ModelState.AddModelError("Lec_Pass_Confirm", "The password you entered does not match the above password."); } else if (f.Lec_Pass.Length < 8) { ModelState.AddModelError("Lec_Pass", "Your password must more than 8 characters."); } else { Lecturer lec = new Lecturer(); lec.Lec_ID = f.Lec_ID; lec.Lec_Name = f.Lec_Name; lec.Lec_Pass = f.Lec_Pass; lec.Lec_Email = f.Lec_Email; lec.Lec_PhoneNo = f.Lec_PhoneNo; db.Lecturers.Add(lec); db.SaveChanges(); return(RedirectToAction("Login")); } } return(View()); }
public ActionResult RegisterLec() { string connectionString = @"Data Source=msi;Initial Catalog=SEFASSIGNMENT;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"; System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(connectionString); sqlConnection.Open(); System.Data.SqlClient.SqlCommand sqlCommand = new System.Data.SqlClient.SqlCommand("SELECT COUNT(*) FROM Lecturer"); sqlCommand.Connection = sqlConnection; int count = Convert.ToInt32(sqlCommand.ExecuteScalar()) + 1; string newID = "LEC" + count.ToString("000"); Lecturer existLec = db.Lecturers.Find(newID); while (existLec != null) { count++; newID = "LEC" + count.ToString("000"); existLec = db.Lecturers.Find(newID); } var model = new RegisterLec { Lec_ID = newID }; return(View(model)); }