private void BTNSearch_Click(object sender, RoutedEventArgs e) { if (BookID.Text == "" && StudentID.Text == "") { MessageBox.Show("Input value for search"); } else if (BookID.Text != "" && StudentID.Text != "") { if (db.AssignBooks.ToList().Exists(p => p.BookId == Int32.Parse(BookID.Text) && p.UserId == Int32.Parse(StudentID.Text) && !p.Status)) { AssignBooks assign = db.AssignBooks.ToList().FirstOrDefault(p => p.BookId == Int32.Parse(BookID.Text) && p.UserId == Int32.Parse(StudentID.Text) && !p.Status); TBlockBookID.Text = assign.BookId.ToString(); TBlockReturnDate.Text = assign.EndDate.ToString(); TBlockStartDate.Text = assign.StartDate.ToString(); TBlockStudentID.Text = assign.UserId.ToString(); var pen = ((assign.EndDate - assign.StartDate).TotalDays) - 15; if (pen > 0) { pen *= 5; TBlockPenality.Text = pen.ToString(); try { db.SaveChanges(); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } } else { TBlockPenality.Text = "0"; } } else { MessageBox.Show("This User not assigned this Book"); } } }
private void BTNAssignBook_Click(object sender, RoutedEventArgs e) { if (FBook && FStud) { Books book = new Books(); AssignBooks assign = new AssignBooks(); foreach (var item in db.Books.ToList()) { if (item.Id == Int32.Parse(TxtBoxBookFind.Text)) { book = item; break; } } if (book.BookQuantity >= 1) { foreach (var item in db.Books.ToList()) { if (item.Id == book.Id) { item.BookQuantity -= 1; break; } } try { assign.BookId = book.Id; assign.UserId = Int32.Parse(TxtBoxStudFind.Text); assign.StartDate = Convert.ToDateTime(TxtBoxBookAssigningDate.Text); assign.EndDate = Convert.ToDateTime(TxtBoxReturnDate.Text); assign.Penality = 0; assign.Status = false; db.AssignBooks.Add(assign); db.SaveChanges(); FBook = false; FStud = false; MessageBox.Show("Book Successufully Assigned "); } catch (Exception ex) { foreach (var item in db.Books.ToList()) { if (item.Id == book.Id) { item.BookQuantity += 1; break; } } MessageBox.Show("Error: " + ex.Message); } } else { MessageBox.Show("Insufficient Quantity Of Books"); } } else if (!FBook) { MessageBox.Show("Enter Book ID and then press button \"Find Book\""); } else if (!FStud) { MessageBox.Show("Enter Student ID and then press button \"Find Student\""); } }