private void tsbSave_Click(object sender, EventArgs e) { try { Cursor.Current = Cursors.WaitCursor; if (txtOR.Text.Trim().Equals("") || txtCash.Text.Trim().Equals("") || !Double.TryParse(txtCash.Text.Trim(), out double res)) { throw new Exception("Please fill important fields."); } List <PaymentDetail> paymentItems = new List <PaymentDetail>(); foreach (DataGridViewRow row in dgvBalance.Rows) { string value = (string)row.Cells["txtPayment"].Value; if (!string.IsNullOrWhiteSpace(value)) { if (!Double.TryParse(value, out double val)) { dgvBalance.CurrentCell = row.Cells["txtPayment"]; dgvBalance.BeginEdit(true); throw new Exception("Invalid amount."); } if (Convert.ToDouble(row.Cells["txtPayment"].Value) > Convert.ToDouble(row.Cells["BALANCE"].Value)) { dgvBalance.CurrentCell = row.Cells["txtPayment"]; dgvBalance.BeginEdit(true); throw new Exception("Amount entered is higher than the balance."); } if (Convert.ToDouble(row.Cells["txtPayment"].Value) > 0) { PaymentDetail feeObj = new PaymentDetail(); feeObj.Code = row.Cells["CODE"].Value.ToString(); feeObj.Amount = Convert.ToDouble(row.Cells["txtPayment"].Value); paymentItems.Add(feeObj); } } } double paymentTotal = 0; if (paymentItems.Count > 0 && Double.TryParse(lblTotal.Text.Trim(), out paymentTotal) && paymentTotal > 0) { int paymenttype = 0; if (cmbPaymentType.SelectedIndex == 0) { paymenttype = 1; } else if (cmbPaymentType.SelectedIndex == 1) { paymenttype = 2; } else if (cmbPaymentType.SelectedIndex == 2) { paymenttype = 3; } else if (cmbPaymentType.SelectedIndex == 3) { paymenttype = 4; } Payment payment = new Payment(id_assessment, txtOR.Text.Trim(), Convert.ToDouble(lblTotal.Text.Trim()), (MdiParent as frmMDI).LoginUser.Username, paymenttype); payment.PaymentDetail = paymentItems; if (payment.SavePayment()) { DialogResult dlg = MessageBox.Show("Payment save successful.\nPlease insert Official Receipt(OR) for printing", "Payment", MessageBoxButtons.OK, MessageBoxIcon.Question); if (dlg == DialogResult.OK) { frmReport frReport; if (cmbPaymentType.SelectedIndex == 0) { frReport = new frmReport(ReportTypes.OR); } else if (cmbPaymentType.SelectedIndex == 1 || cmbPaymentType.SelectedIndex == 3) { frReport = new frmReport(ReportTypes.OFOR); } else { frReport = new frmReport(ReportTypes.PTAOR); } frReport.Payer = Payer; frReport.ORNumber = txtOR.Text.Trim(); frReport.MdiParent = MdiParent; frReport.Show(); } tsbClear.PerformClick(); } else { throw new Exception("Error in saving this payment."); } } Cursor.Current = Cursors.Default; } catch (MySqlException ex) { if (ex.Number == 1042) { MessageBox.Show("Database server is offline. Contact administrator.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show(ex.Number + ": " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception ex) { Cursor.Current = Cursors.Default; MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }