Example #1
0
        public void createStudent(string firstName, string lastName/*, string indexNo*/, int groupId)
        {
            try
            {
                using (var db = new StorageContext())
                {
                    var group = db.Groups.Find(groupId);
                    var student = new Student
                    {
                        FirstName = firstName,
                        LastName = lastName,
                        Group = group
                    };

                    db.Students.Add(student);
                    db.SaveChanges();
                    student.IndexNo = Convert.ToString(student.StudentId);
                    db.SaveChanges();

                }
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var errors in dbEx.EntityValidationErrors)
                {
                    foreach (var error in errors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property name: {0} Error: {1}", error.PropertyName, error.ErrorMessage);
                    }
                }
            }
        }
Example #2
0
        public void deleteStudent(Student student)
        {
            try
            {
                using (var db = new StorageContext())
                {
                    var oldStudent = db.Students.Find(student.StudentId);
                    if (oldStudent != null)
                    {
                        db.Students.Remove(oldStudent);
                        db.SaveChanges();
                    }

                }
            }
            catch (Exception e)
            {
                Console.Write(e.ToString());
            }
        }
Example #3
0
 public void updateStudent(Student newStudent)
 {
     using (var db = new StorageContext())
     {
         var oldStudent = db.Students.Find(newStudent.StudentId);
         if (oldStudent != null)
         {
             oldStudent.FirstName = newStudent.FirstName;
             oldStudent.LastName = newStudent.LastName;
             try
             {
                 db.SaveChanges();
             }
             catch (DbEntityValidationException dbEx)
             {
                 foreach (var errors in dbEx.EntityValidationErrors)
                 {
                     foreach (var error in errors.ValidationErrors)
                     {
                         Trace.TraceInformation("Property name: {0} Error: {1}", error.PropertyName, error.ErrorMessage);
                     }
                 }
             }
         }
     }
 }