public int Insert(MaterialModal material) { int rowsAffected = 0; using (SqlConnection connection = new SqlConnection(_connectionString)) { connection.Open(); SqlCommand com = new SqlCommand("spInsertMaterial", connection); com.CommandType = CommandType.StoredProcedure; com.Parameters.AddWithValue("@SessionId", material.SessionId); com.Parameters.AddWithValue("@SemesterId", material.SemesterId); com.Parameters.AddWithValue("@CourseId", material.CourseId); com.Parameters.AddWithValue("@TeacherId", material.TeacherId); com.Parameters.AddWithValue("@MaterialName", material.MaterialName); com.Parameters.AddWithValue("@MaterialDescription", material.MaterialDescription); try { rowsAffected = com.ExecuteNonQuery(); } catch (Exception e) { return(0); } return(rowsAffected); } }
public JsonResult Insert(MaterialModal material) { bool isExist = _databaseConnection.CheckDuplication(material); if (isExist) { return(Json('0', JsonRequestBehavior.AllowGet)); } return(Json(_databaseConnection.Insert(material))); }
public bool CheckDuplication(MaterialModal material) { List <MaterialModal> listOfMaterials = new List <MaterialModal>(); using (SqlConnection connection = new SqlConnection(_connectionString)) { connection.Open(); SqlCommand com = new SqlCommand("spListMaterial", connection); com.CommandType = CommandType.StoredProcedure; SqlDataReader rdr = com.ExecuteReader(); while (rdr.Read()) { listOfMaterials.Add(new MaterialModal { Id = Int32.Parse(rdr["Id"].ToString()), SessionId = Int32.Parse(rdr["SessionId"].ToString()), SemesterId = Int32.Parse(rdr["SemesterId"].ToString()), CourseId = Int32.Parse(rdr["CourseId"].ToString()), TeacherId = Int32.Parse(rdr["TeacherId"].ToString()), MaterialName = rdr["MaterialName"].ToString(), MaterialDescription = rdr["MaterialDescription"].ToString() }); } } foreach (MaterialModal materialOfSubject in listOfMaterials) { if ((materialOfSubject.SessionId == material.SessionId && materialOfSubject.SemesterId == material.SemesterId && materialOfSubject.CourseId == material.CourseId && materialOfSubject.TeacherId == material.TeacherId && materialOfSubject.MaterialName.Equals(material.MaterialName)) && materialOfSubject.MaterialDescription.Equals(material.MaterialDescription)) { return(true); } } return(false); }