Exemplo n.º 1
0
 private void btnDelete_Click(object sender, EventArgs e)
 {
     if (!DeleteGotEmptyField() && IsMidExist2() && !DeleteGotEmptyField())
     {
         if (MessageBox.Show("Confirm to delete?", "Delete Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
         {
             var       mid     = Convert.ToInt32(txtMIDDelete.Text);
             Member    nMember = context.Members.Where(x => x.MemberID == mid).First();
             IssueTran issue   = context.IssueTrans.Where(x => x.MemberID == mid).First();
             context.IssueTrans.Remove(issue);
             context.Members.Remove(nMember);
             MessageBox.Show("Delete Successfully.");
         }
     }
     context.SaveChanges();
 }
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            lblMIDWarning.Visible = false;
            if (hasFieldEmpty() == true)
            {
                MessageBox.Show("All fields must be filled/selected. Please indicate NA if no remarks to be made. ");
            }
            else
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    try
                    {
                        IssueTran IT = context.IssueTrans.Where(x => x.TransactionID == TransactionID).First();
                        IT.DateActualReturn  = DateActualReturn;
                        IT.LoanStatus        = "IN";
                        IT.Remarks           = txtRemark.Text;
                        IT.OverdueFine       = fineAmount;
                        IT.FinePaymentStatus = cboBoxPaymentStatus.SelectedItem.ToString();
                        IT.BookID            = bookID;

                        Member      ML = context.Members.Where(x => x.MemberID == MemberID).First();
                        BookListing BL = context.BookListings.Where(x => x.BookID == bookID).First();

                        if (BL.NumberLoaned > 0 && ML.CurrentNumberOnLoan > 0)
                        {
                            BL.NumberLoaned--;
                            ML.CurrentNumberOnLoan--;

                            MessageBox.Show("Book Return is successful.");
                        }
                        else
                        {
                            MessageBox.Show("There is an error in the number of books on loan. Please check on transaction record.");
                        }

                        context.SaveChanges();
                        ts.Complete();
                    }
                    catch (TransactionException ex)
                    {
                        ts.Dispose();
                        MessageBox.Show("Transaction Exception Occured");
                    }
                }
            }
        }
        private void CommitTransaction()
        {
            using (TransactionScope ts = new TransactionScope())
            {
                // 3. Update IssueTrans table
                IssueTran it = new IssueTran();
                it.MemberID          = Int32.Parse(txtBoxMemberID.Text);
                it.BookID            = txtBoxBookID.Text;
                it.DateBorrow        = dtpBorrow.Value.Date;
                it.DateDue           = dtpDue.Value.Date;
                it.LoanStatus        = "OUT";
                it.Remarks           = "NIL";
                it.OverdueFine       = 0;
                it.FinePaymentStatus = "NA";
                context.IssueTrans.Add(it);

                //4. Update Member table : CurentNumberOnLoan
                Member m = context.Members
                           .Where(x => x.MemberID.ToString() == txtBoxMemberID.Text)
                           .First();

                int newNumberOnLoan = context
                                      .Members
                                      .Where(x => x.MemberID.ToString() == txtBoxMemberID.Text)
                                      .Select(x => x.CurrentNumberOnLoan).First() + 1;

                m.CurrentNumberOnLoan = newNumberOnLoan;


                //5. Update Book Listing table: NumberLoaned ++
                BookListing b = context.BookListings
                                .Where(x => x.BookID.ToString() == txtBoxBookID.Text)
                                .First();
                b.NumberLoaned++;

                //Commit Changes
                context.SaveChanges();

                ts.Complete();
            }
        }