private async void AddStudentButton_Click(object sender, RoutedEventArgs e)
        {
            Course course;

            try
            {
                course = new Course
                {
                    Code   = this.CourseCodeTextBox.Text,
                    Credit = int.Parse(this.CourseCreditTextBox.Text),
                    Quota  = int.Parse(this.CourseQuotaTextBox.Text)
                };
            }
            catch
            {
                MessageBox.Show("Please enter valid values for course. (Credit and Quota must be a number)");

                return;
            }

            await _context.AddAsync(course);

            await _context.SaveChangesAsync();

            MessageBox.Show("Course is added successfully.");

            _grid.ItemsSource = await _context.Courses.ToListAsync();
        }
        public async Task <Student> AddStudent(Student student)
        {
            if (student == null)
            {
                throw new ArgumentNullException($"Student must not be null");
            }

            try
            {
                await SchoolDbContext.AddAsync(student);

                await SchoolDbContext.SaveChangesAsync();

                return(student);
            }
            catch (Exception ex)
            {
                throw new Exception($"Student could not be saved: {ex.Message}");
            }
        }
        private async void AddStudentButton_Click(object sender, RoutedEventArgs e)
        {
            Student student = new Student
            {
                BirthDate = this.StudentBirthDate.DisplayDate,
                Name      = this.StudentNameTextBox.Text,
                StudentId = this.StudentIdTextBox.Text
            };

            await _context.AddAsync(student);

            await _context.SaveChangesAsync();

            MessageBox.Show("Student is added successfully.");

            _grid.ItemsSource = await _context.Students.ToListAsync();
        }
Example #4
0
        public async virtual Task <T> AddAsync(T entity)
        {
            try
            {
                var addedEntity = await dbContext.AddAsync <T>(entity);

                return(addedEntity.Entity);
            }
            catch (DbUpdateConcurrencyException e)
            {
                var errorMessage = ProcessException("Add db update concurrency error: ",
                                                    e.Message,
                                                    e.InnerException,
                                                    ErrorType.Critical);
                throw new DataAccessException(errorMessage);
            }
            catch (DbUpdateException e)
            {
                var errorMessage = ProcessException("Add db update error: ",
                                                    e.Message,
                                                    e.InnerException,
                                                    ErrorType.Critical);
                throw new DataAccessException(errorMessage);
            }
            catch (InvalidOperationException e)
            {
                var errorMessage = ProcessException("Add db invalid operation error: ",
                                                    e.Message,
                                                    e.InnerException,
                                                    ErrorType.Critical);
                throw new DataAccessException(errorMessage);
            }
            catch (Exception e)
            {
                var errorMessage = ProcessException("Add db general error: ",
                                                    e.Message,
                                                    e.InnerException,
                                                    ErrorType.Critical);
                throw new DataAccessException(errorMessage);
            }
        }
Example #5
0
        public async Task CreateCourse(Course course)
        {
            await _context.AddAsync(course);

            await _context.SaveChangesAsync();
        }