private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (decimal.Parse(lblTotalSOQty.Text) == 0)
                {
                    MessageBoxUI _mb1 = new MessageBoxUI("Totals of S.O. Qty must not be Zero(0)!", GlobalVariables.Icons.Error, GlobalVariables.Buttons.OK);
                    _mb1.showDialog();
                    return;
                }
                string _CustomerId  = "";
                string _SalesPerson = "";
                try
                {
                    _CustomerId = cboCustomer.SelectedValue.ToString();
                }
                catch
                {
                    MessageBoxUI _mb1 = new MessageBoxUI("You must select a Customer!", GlobalVariables.Icons.Error, GlobalVariables.Buttons.OK);
                    _mb1.showDialog();
                    cboCustomer.Focus();
                    return;
                }
                //check credit limit
                decimal _CreditLimit         = 0;
                decimal _TotalRunningBalance = 0;
                foreach (DataRow _drCL in loCustomer.getCustomerCreditLimit(_CustomerId).Rows)
                {
                    _CreditLimit = decimal.Parse(_drCL["CreditLimit"].ToString());
                }
                foreach (DataRow _drRB in loSalesOrder.getTotalRunningBalance(_CustomerId).Rows)
                {
                    _TotalRunningBalance = decimal.Parse(_drRB["RunningBalance"].ToString());
                }
                if ((decimal.Parse(txtTotalAmount.Text) + _TotalRunningBalance) > _CreditLimit)
                {
                    MessageBoxUI _mb1 = new MessageBoxUI("Total Amount + Running Balance exceeds Credit Limit of " + string.Format("{0:n}", _CreditLimit) + "!", GlobalVariables.Icons.Error, GlobalVariables.Buttons.OK);
                    _mb1.showDialog();
                    return;
                }

                try
                {
                    _SalesPerson = cboSalesPerson.SelectedValue.ToString();
                }
                catch
                {
                    _SalesPerson = "";
                }
                DialogResult _dr = new DialogResult();
                MessageBoxUI _mb = new MessageBoxUI("Continue saving this Sales Order?", GlobalVariables.Icons.QuestionMark, GlobalVariables.Buttons.YesNo);
                _mb.ShowDialog();
                _dr = _mb.Operation;
                if (_dr == DialogResult.Yes)
                {
                    loSalesOrder.Id   = lSalesOrderId;
                    loSalesOrder.Date = dtpDate.Value;
                    try
                    {
                        loSalesOrder.PQId = cboPriceQuotation.SelectedValue.ToString();
                    }
                    catch
                    {
                        loSalesOrder.PQId = "";
                    }

                    loSalesOrder.Reference  = GlobalFunctions.replaceChar(txtReference.Text);
                    loSalesOrder.CustomerId = _CustomerId;
                    try
                    {
                        loSalesOrder.Terms = int.Parse(txtTerms.Text);
                    }
                    catch
                    {
                        loSalesOrder.Terms = 0;
                    }
                    loSalesOrder.DueDate          = dtpDueDate.Value;
                    loSalesOrder.Instructions     = GlobalFunctions.replaceChar(txtInstructions.Text);
                    loSalesOrder.SalesPersonId    = _SalesPerson;
                    loSalesOrder.TotalSOQty       = decimal.Parse(lblTotalSOQty.Text);
                    loSalesOrder.TotalQtyOut      = decimal.Parse(lblTotalQtyOut.Text);
                    loSalesOrder.TotalQtyVariance = decimal.Parse(lblTotalQtyVariance.Text);
                    loSalesOrder.TotalAmount      = decimal.Parse(txtTotalAmount.Text);
                    loSalesOrder.Remarks          = GlobalFunctions.replaceChar(txtRemarks.Text);
                    loSalesOrder.UserId           = GlobalVariables.UserId;
                    try
                    {
                        string _SalesOrderId = loSalesOrder.save(lOperation);
                        if (_SalesOrderId != "")
                        {
                            for (int i = 0; i < dgvDetailStockInventory.Rows.Count; i++)
                            {
                                if (dgvDetailStockInventory.Rows[i].Cells["Status"].Value.ToString() == "Add")
                                {
                                    try
                                    {
                                        loSalesOrderDetail.DetailId = dgvDetailStockInventory.Rows[i].Cells["Id"].Value.ToString();
                                    }
                                    catch
                                    {
                                        loSalesOrderDetail.DetailId = "";
                                    }
                                    try
                                    {
                                        loSalesOrderDetail.SalesOrderId   = _SalesOrderId;
                                        loSalesOrderDetail.StockId        = dgvDetailStockInventory.Rows[i].Cells["StockId"].Value.ToString();
                                        loSalesOrderDetail.LocationId     = dgvDetailStockInventory.Rows[i].Cells["LocationId"].Value.ToString();
                                        loSalesOrderDetail.SOQty          = decimal.Parse(dgvDetailStockInventory.Rows[i].Cells["SOQty"].Value.ToString());
                                        loSalesOrderDetail.QtyOut         = decimal.Parse(dgvDetailStockInventory.Rows[i].Cells["QtyOut"].Value.ToString());
                                        loSalesOrderDetail.QtyVariance    = decimal.Parse(dgvDetailStockInventory.Rows[i].Cells["QtyVariance"].Value.ToString());
                                        loSalesOrderDetail.UnitPrice      = decimal.Parse(dgvDetailStockInventory.Rows[i].Cells["UnitPrice"].Value.ToString());
                                        loSalesOrderDetail.DiscountId     = dgvDetailStockInventory.Rows[i].Cells["DiscountId"].Value.ToString();
                                        loSalesOrderDetail.DiscountAmount = decimal.Parse(dgvDetailStockInventory.Rows[i].Cells["DiscountAmount"].Value.ToString());
                                        loSalesOrderDetail.TotalPrice     = decimal.Parse(dgvDetailStockInventory.Rows[i].Cells["TotalPrice"].Value.ToString());
                                        loSalesOrderDetail.Remarks        = dgvDetailStockInventory.Rows[i].Cells["Remarks"].Value.ToString();
                                        loSalesOrderDetail.UserId         = GlobalVariables.UserId;
                                        loSalesOrderDetail.save(GlobalVariables.Operation.Add);
                                    }
                                    catch { }
                                }
                                else if (dgvDetailStockInventory.Rows[i].Cells["Status"].Value.ToString() == "Edit")
                                {
                                    try
                                    {
                                        loSalesOrderDetail.DetailId = dgvDetailStockInventory.Rows[i].Cells["Id"].Value.ToString();
                                    }
                                    catch
                                    {
                                        loSalesOrderDetail.DetailId = "";
                                    }
                                    try
                                    {
                                        loSalesOrderDetail.SalesOrderId   = _SalesOrderId;
                                        loSalesOrderDetail.StockId        = dgvDetailStockInventory.Rows[i].Cells["StockId"].Value.ToString();
                                        loSalesOrderDetail.LocationId     = dgvDetailStockInventory.Rows[i].Cells["LocationId"].Value.ToString();
                                        loSalesOrderDetail.SOQty          = decimal.Parse(dgvDetailStockInventory.Rows[i].Cells["SOQty"].Value.ToString());
                                        loSalesOrderDetail.QtyOut         = decimal.Parse(dgvDetailStockInventory.Rows[i].Cells["QtyOut"].Value.ToString());
                                        loSalesOrderDetail.QtyVariance    = decimal.Parse(dgvDetailStockInventory.Rows[i].Cells["QtyVariance"].Value.ToString());
                                        loSalesOrderDetail.UnitPrice      = decimal.Parse(dgvDetailStockInventory.Rows[i].Cells["UnitPrice"].Value.ToString());
                                        loSalesOrderDetail.DiscountId     = dgvDetailStockInventory.Rows[i].Cells["DiscountId"].Value.ToString();
                                        loSalesOrderDetail.DiscountAmount = decimal.Parse(dgvDetailStockInventory.Rows[i].Cells["DiscountAmount"].Value.ToString());
                                        loSalesOrderDetail.TotalPrice     = decimal.Parse(dgvDetailStockInventory.Rows[i].Cells["TotalPrice"].Value.ToString());
                                        loSalesOrderDetail.Remarks        = dgvDetailStockInventory.Rows[i].Cells["Remarks"].Value.ToString();
                                        loSalesOrderDetail.UserId         = GlobalVariables.UserId;
                                        loSalesOrderDetail.save(GlobalVariables.Operation.Edit);
                                    }
                                    catch { }
                                }
                                else if (dgvDetailStockInventory.Rows[i].Cells["Status"].Value.ToString() == "Delete")
                                {
                                    loSalesOrderDetail.remove(dgvDetailStockInventory.Rows[i].Cells[0].Value.ToString());
                                }
                            }
                            if (txtId.Text == _SalesOrderId)
                            {
                                MessageBoxUI _mb2 = new MessageBoxUI("Sales Order has been saved successfully!", GlobalVariables.Icons.Save, GlobalVariables.Buttons.OK);
                                _mb2.showDialog();
                            }
                            else
                            {
                                MessageBoxUI _mb2 = new MessageBoxUI("Sales Order has been saved successfully! New Sales Order ID. : " + _SalesOrderId, GlobalVariables.Icons.Save, GlobalVariables.Buttons.OK);
                                _mb2.showDialog();
                            }

                            //previewDetail(_SalesOrderId);

                            object[] _params = { };
                            ParentList.GetType().GetMethod("refresh").Invoke(ParentList, _params);
                            this.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message.Contains("Unclosed quotation mark after the character string"))
                        {
                            MessageBoxUI _mb3 = new MessageBoxUI("Do not use this character( ' ).", GlobalVariables.Icons.Error, GlobalVariables.Buttons.OK);
                            _mb3.showDialog();
                            return;
                        }
                        else
                        {
                            MessageBoxUI _mb3 = new MessageBoxUI(ex.Message, GlobalVariables.Icons.Error, GlobalVariables.Buttons.OK);
                            _mb3.showDialog();
                            return;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorMessageUI em = new ErrorMessageUI(ex.Message, this.Name, "btnSave_Click");
                em.ShowDialog();
                return;
            }
        }