private void BtnSubmit_Click(object sender, EventArgs e)
        {
            tblCustomerPayment tbv = new tblCustomerPayment();

            tbv.CustomerId      = Convert.ToInt32(cboCustomer.SelectedValue.ToString());
            tbv.PaymentDate     = Convert.ToDateTime(dateTimePicker1.Value);
            tbv.TotalAmount     = Convert.ToDecimal(txtTotalAmount.Text);
            tbv.PaymentAmount   = Convert.ToDecimal(txtPaidAmount.Text);
            tbv.RemainingAmount = Convert.ToDecimal(txtRemainingAmount.Text);
            tbv.PaymentMode     = cboPaymentMode.Text;
            _db.tblCustomerPayments.Add(tbv);
            if (_db.SaveChanges() > 0)
            {
                int customerid = Convert.ToInt32(cboCustomer.SelectedValue.ToString());
                var vendors    = _db.tblCustomerPayments.Select(x => new { CustomerId = x.CustomerId, CustomerName = x.tblCustomer.Name, PaymentDate = x.PaymentDate, TotalAmount = x.TotalAmount, PaymentAmount = x.PaymentAmount, RemainingAmount = x.RemainingAmount }).Where(v => v.CustomerId == customerid).ToList();

                dataGridView1.DataSource = vendors;

                MessageBox.Show("Payment Done");
                cboCustomer.SelectedIndex    = 0;
                txtPaidAmount.Text           = "";
                txtTotalAmount.Text          = "";
                txtRemainingAmount.Text      = "";
                cboPaymentMode.SelectedIndex = 0;
            }
        }
        private void BtnSave_Click(object sender, EventArgs e)
        {
            tblSalesInvoice tb = new tblSalesInvoice();

            tb.InvoiceNo   = txtInvoiceNo.Text;
            tb.InvoiceDate = Convert.ToDateTime(txtInvoiceDate.Text);
            tb.GrandTotal  = Convert.ToDecimal(txtGrandTotal.Text);
            tb.CustomerId  = customerid;
            db.tblSalesInvoices.Add(tb);
            db.SaveChanges();
            tblCustomerPayment tbvenpay = new tblCustomerPayment();

            tbvenpay.CustomerId      = customerid;
            tbvenpay.PaymentDate     = Convert.ToDateTime(txtInvoiceDate.Text);
            tbvenpay.TotalAmount     = Convert.ToDecimal(txtGrandTotal.Text);
            tbvenpay.PaymentAmount   = Convert.ToDecimal(txtPaidAmount.Text);
            tbvenpay.RemainingAmount = Convert.ToDecimal(txtRemainingAmount.Text);

            tbvenpay.PaymentMode = rbdCash.Checked ? "Cash" : "Cheque";
            db.tblCustomerPayments.Add(tbvenpay);

            if (db.SaveChanges() > 0)
            {
                foreach (DataGridViewRow dr in dataGridView1.Rows)
                {
                    tblSale pur = new tblSale();
                    pur.SalesInvoiceId = tb.SalesInvoiceId;
                    pur.ProductId      = Convert.ToInt32(dr.Cells["colProductId"].Value);

                    pur.Quantity  = Convert.ToInt32(dr.Cells["colQuantity"].Value);
                    pur.UnitPrice = Convert.ToDecimal(dr.Cells["colUnitPrice"].Value);
                    pur.Total     = Convert.ToDecimal(dr.Cells["colTotal"].Value);
                    pur.SalesDate = Convert.ToDateTime(txtInvoiceDate.Text);
                    pur.Tax       = Convert.ToDecimal(dr.Cells["colTax"].Value);
                    db.tblSales.Add(pur);
                    if (db.SaveChanges() > 0)
                    {
                        tblStock tbst = db.tblStocks.Where(p => p.ProductId == pur.ProductId).FirstOrDefault();
                        if (tbst != null)
                        {
                            int puranoquantity = Convert.ToInt32(tbst.Quantity);
                            tbst.Quantity = puranoquantity - Convert.ToInt32(dr.Cells["colQuantity"].Value);
                            db.SaveChanges();
                        }
                    }
                }
            }
            MessageBox.Show("Sales Done");
        }