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");
        }
        private void FrmSales_Load(object sender, EventArgs e)
        {
            LoadCategory();
            LoadCategory();
            txtInvoiceDate.Text = DateTime.Today.ToShortDateString();


            tblSalesInvoice pur = db.tblSalesInvoices.OrderByDescending(i => i.SalesInvoiceId).Take(1).FirstOrDefault();

            if (pur != null)
            {
                string[] invno = pur.InvoiceNo.Split('-');
                int      maxno = Convert.ToInt32(invno[1]) + 1;
                txtInvoiceNo.Text = "SL-" + maxno;
            }
            else
            {
                txtInvoiceNo.Text = "SL-1";
            }
        }