private void btnItemDel_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.Rows.Count >= 0)
                {
                    if (checkBillExist() == false)
                    {
                        dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);

                        //THIS WILL RE-NUMBER THE ROW WHEN DELETED
                        sequenceNumber();
                        calculateTotal();
                    }
                    else
                    {
                        if (MessageBox.Show("Are You Sure to Delete this Record ?", " Warning!", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            string getProductName   = dataGridView1.CurrentRow.Cells[1].FormattedValue.ToString();
                            int    getstockQuantity = Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].FormattedValue.ToString());
                            string getSaleTotal     = dataGridView1.CurrentRow.Cells[4].FormattedValue.ToString();

                            var itemToRemove = new object();

                            using (DBEntities db = new DBEntities())
                            {
                                itemToRemove = db.sales.Where(x => x.billNumber == txtBill.Text.Trim() && x.saleName == getProductName).FirstOrDefault(); //returns a single item.
                                var itemToRemove1 = db.sales.Where(x => x.billNumber == txtBill.Text.Trim() && x.saleName == getProductName).FirstOrDefault();;
                                //ITEM WILL BE REMOVED FROM DATABASE

                                db.sales.Remove(itemToRemove1);
                                db.SaveChanges();
                                sequenceNumber();
                                calculateTotal();
                            }

                            if (itemToRemove != null)
                            {
                                using (DBEntities db = new DBEntities())
                                {
                                    //UPDATE STOCK QUANTITY AFTER DELETE ON STOCK TABLE

                                    var result = db.Stocks.SingleOrDefault(b => b.stockName == getProductName);

                                    if (result != null)
                                    {
                                        result.stockQuantity += getstockQuantity;

                                        db.SaveChanges();
                                    }
                                }

                                updateSaleQandT();
                                updateBillSale(getSaleTotal);



                                dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
                            }
                            else
                            {
                                //REMOVE ITEM FROM DATAGRIDVIEW NOT FROM DATABASE

                                dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
                                sequenceNumber();
                                calculateTotal();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show("THIS ROW CAN NOT BE DELETED", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show(ex.ToString());
            }
        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            string vim = txtBill.Text.Trim();

            try
            {
                if (checkBillExist() == false)
                {
                    //INSERT Bill Number into the database

                    billy.billNumber = vim;
                    using (DBEntities db = new DBEntities())
                    {
                        db.bills.Add(billy);
                        db.SaveChanges();
                    }



                    //using (DBEntities db = new DBEntities())
                    //{
                    //    billy = db.bills.Where(x => x.billNumber == vim).FirstOrDefault();
                    //}


                    //INSERT INTO BILL SALES

                    bls.saleName    = txtRep.Text;
                    bls.totalAmount = txtTotal.Text;
                    bls.billINumber = vim;


                    using (DBEntities db = new DBEntities())
                    {
                        db.billSales.Add(bls);
                        db.SaveChanges();
                    }


                    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                    {
                        //THIS WILL DEDUCT THE SOLD QUANTITY FROM STOCK QUANTITY AND UPDATE THE DATABASE

                        string productName = dataGridView1.Rows[i].Cells[1].FormattedValue.ToString();
                        //     int billNum = Convert.ToInt32(vim);

                        using (DBEntities db = new DBEntities())
                        {
                            var updateQuery = (from stock in db.Stocks
                                               where stock.stockName == productName
                                               select stock).FirstOrDefault();



                            if (updateQuery.stockQuantity > 0)
                            {
                                updateQuery.stockQuantity -= Convert.ToInt32(dataGridView1.Rows[i].Cells[3].FormattedValue);
                                //  MessageBox.Show(updateQuery.stockQuantity.ToString());
                                db.SaveChanges();
                            }
                        }
                        //////////////////////////////////////////////////////////////////


                        //SAVE DATAGRIDVIEW TO THE SALES DATABASE

                        using (DBEntities db = new DBEntities())
                        {
                            var getStockId = (from stock in db.Stocks
                                              where stock.stockName == productName
                                              select stock).FirstOrDefault();



                            saley.saleName      = dataGridView1.Rows[i].Cells[1].FormattedValue.ToString();
                            saley.salePrice     = dataGridView1.Rows[i].Cells[2].FormattedValue.ToString();
                            saley.saleQuantity  = Convert.ToInt32(dataGridView1.Rows[i].Cells[3].FormattedValue);
                            saley.saleTotal     = dataGridView1.Rows[i].Cells[4].FormattedValue.ToString();
                            saley.stockId       = getStockId.stockId;
                            saley.billNumber    = vim;
                            saley.totalAmount   = txtTotal.Text;
                            saley.totalQuantity = Convert.ToInt32(txtQuan.Text);
                            saley.repName       = txtRep.Text;


                            db.sales.Add(saley);
                            db.SaveChanges();
                        }
                    }
                    clearGridView();
                    sequenceNumber();
                    generateBill();
                    MessageBox.Show("SOLD!!");
                }
                else
                {
                    //UPDATE TOTAL AMOUNT IN THE BILL SALES

                    using (DBEntities db = new DBEntities())
                    {
                        var result = db.billSales.SingleOrDefault(b => b.billINumber == txtBill.Text.Trim());

                        if (result != null)
                        {
                            result.totalAmount = txtTotal.Text.Trim();

                            db.SaveChanges();
                        }
                    }

                    //DELETE ALL ROWS IN THE SALES TABLE ONE BY ONE


                    int    getstockQuantity = Convert.ToInt32(dataGridView1.CurrentRow.Cells[3].FormattedValue.ToString());
                    string getSaleTotal     = dataGridView1.CurrentRow.Cells[4].FormattedValue.ToString();

                    var itemToRemove = new object();

                    using (DBEntities db = new DBEntities())
                    {
                        //itemToRemove = db.sales.Where(x => x.billNumber == txtBill.Text.Trim() && x.saleName == getProductName).FirstOrDefault(); //returns a single item.
                        var itemToRemove1 = db.sales.Where(x => x.billNumber == txtBill.Text.Trim());
                        //ITEM WILL BE REMOVED FROM DATABASE

                        db.sales.RemoveRange(itemToRemove1);
                        db.SaveChanges();
                        sequenceNumber();
                        calculateTotal();
                    }



                    //INSERT THE NEW ROWS INTO THE SALE DATABASE AFRESH

                    for (int k = 0; k < dataGridView1.Rows.Count - 1; k++)
                    {
                        string productName = dataGridView1.Rows[k].Cells[1].FormattedValue.ToString();
                        using (DBEntities db = new DBEntities())
                        {
                            var getStockId = (from stock in db.Stocks
                                              where stock.stockName == productName
                                              select stock).FirstOrDefault();



                            saley.saleName      = dataGridView1.Rows[k].Cells[1].FormattedValue.ToString();
                            saley.salePrice     = dataGridView1.Rows[k].Cells[2].FormattedValue.ToString();
                            saley.saleQuantity  = Convert.ToInt32(dataGridView1.Rows[k].Cells[3].FormattedValue);
                            saley.saleTotal     = dataGridView1.Rows[k].Cells[4].FormattedValue.ToString();
                            saley.stockId       = getStockId.stockId;
                            saley.billNumber    = vim;
                            saley.totalAmount   = txtTotal.Text;
                            saley.totalQuantity = Convert.ToInt32(txtQuan.Text);
                            saley.repName       = txtRep.Text;


                            db.sales.Add(saley);
                            db.SaveChanges();
                        }
                    }
                    clearGridView();
                    sequenceNumber();
                    generateBill();
                    MessageBox.Show("THIS ITEMS WILL BE UPDATED!!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }



            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


            ClsPrint(dataGridView1, "SALES INVOICE" + "  " + "Total Quantity Purchased: " + txtQuan.Text + "  " + "Total Amount:" + txtTotal.Text);
            PrintDialog printDialog = new PrintDialog();

            //Get the document
            if (DialogResult.OK == printDialog.ShowDialog())
            {
                PrintPreviewDialog objPPdialog = new PrintPreviewDialog();

                _printDocument.Print();
            }
        }
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string productName = dataGridView1.CurrentRow.Cells[1].FormattedValue.ToString();

                if (dataGridView1.Rows[e.RowIndex].Cells[1].FormattedValue.ToString() != "")
                {
                    ///LOAD THE PRICE OF THE SELECTED PRODUCT
                    if (dataGridView1.CurrentCell.ColumnIndex == 1)
                    {
                        using (DBEntities db = new DBEntities())
                        {
                            stock = db.Stocks.Where(x => x.stockName == productName).FirstOrDefault();

                            dataGridView1.CurrentRow.Cells[2].Value = stock.stockPrice;
                        }
                    }



                    //CALCUALTE GRIDVIEW FOR TOTAL PRICE AND TOTAL QUANTITY

                    if (dataGridView1.CurrentRow.Cells[3].FormattedValue.ToString() != "")
                    {
                        double final = 0;
                        double quan  = 0;
                        double total = 0;

                        double price    = Convert.ToDouble(dataGridView1.CurrentRow.Cells[2].Value);
                        double quantity = Convert.ToDouble(dataGridView1.CurrentRow.Cells[3].Value);

                        total = price * quantity;
                        dataGridView1.CurrentRow.Cells[4].Value = total.ToString();
                    }

                    ///CHECK IF SELECTED PRODUCT IS OUT OF STOCK
                    ///
                    if (dataGridView1.CurrentCell.ColumnIndex == 3)
                    {
                        string productQuantity = dataGridView1.CurrentRow.Cells[3].FormattedValue.ToString();
                        string totalAmount     = dataGridView1.CurrentRow.Cells[4].FormattedValue.ToString();
                        //string productName = dataGridView1.CurrentRow.Cells[1].Value.ToString();

                        using (DBEntities db = new DBEntities())
                        {
                            stock = db.Stocks.Where(x => x.stockName == productName).FirstOrDefault();

                            if (productQuantity != "")
                            {
                                //MessageBox.Show(stock.stockQuantity.ToString());
                                if (stock.stockQuantity < Convert.ToDecimal(productQuantity))
                                {
                                    MessageBox.Show("OUT OF STOCK!");
                                    dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
                                    //THIS WILL RE-NUMBER THE ROW WHEN DELETED
                                }
                            }
                        }
                    }
                }
                calculateTotal();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }