public int LastAddedInvestlNo()
        {
           
            OrderGateway gateway=new OrderGateway();
            
            DBManager manager = new DBManager();
            SqlConnection connection = manager.Connection();
           

            string selectQuery = "SELECT Order_No From Orders";
            SqlCommand cmd = new SqlCommand(selectQuery, connection);
            connection.Open();  
            SqlDataReader reader = cmd.ExecuteReader();
            List<int> totalId = new List<int>();

            while (reader.Read())
            {
                int aId = Convert.ToInt16(reader[0]);

               
                totalId.Add(aId);
            }
            if (totalId.Count<=0)
            {
                return 0;
            }
            else
            {
                int x = totalId.Max();

                return x;  
            }
           

        }
        //save an Order 
        private void orderAcceptButton_Click(object sender, EventArgs e)
        {

            try
            {
                OrderGateway gateway = new OrderGateway();
                Order anOrder = new Order();
                anOrder.CustomerName = customerNameTextBox.Text;
                anOrder.CustomerPhone = customerMobileTextBox.Text;
                anOrder.BookName = bookNameTextBox.Text;
                anOrder.WriterName = writerNameTextBox.Text;
                anOrder.Edition = bookEditionTextBox.Text;
                anOrder.BuyUnitPrice = Convert.ToDouble(buyingUnitPriceTextBox.Text);
                string quantiy = bookQuantityTextBOx.Text;
                anOrder.Quantity = Convert.ToInt16(quantiy);
                anOrder.UnitPrice = Convert.ToDouble(bookUnitPriceTextBOx.Text);
                anOrder.Advance = Convert.ToDouble(advanceTextBox.Text);
                
                anOrder.SupplyDate = dateTimePicker1.Text;
                string st = gateway.SaveOrder(anOrder);
                MessageBox.Show(st,"Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
                ClearALLtextBox();
                LoadAllBook();

                serialTextBox.Text = LastAddedInvestlNo().ToString();
            }
            catch (Exception)
            {
               
                MessageBox.Show("Please fill every fields properly.", "Error", MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
            }


        }
        //save an Order 
        private void orderAcceptButton_Click(object sender, EventArgs e)
        {
            
            try
            {
                OrderGateway gateway = new OrderGateway();
                Order anOrder = new Order();
                anOrder.CustomerName = customerNameTextBox.Text;
                anOrder.CustomerPhone = customerMobileTextBox.Text;
                anOrder.BookName = bookNameTextBox.Text;
                anOrder.WriterName = writerNameTextBox.Text;
                anOrder.Edition = bookEditionTextBox.Text;
                anOrder.TypeOfBook = typeOfBookTextBox.Text;
                anOrder.BookPrint = bookPrintTextbox.Text;
                anOrder.BuyUnitPrice = Convert.ToDouble(buyingUnitPriceTextBox.Text);
                string quantiy = bookQuantityTextBOx.Text;
                anOrder.Quantity = Convert.ToInt16(quantiy);
                anOrder.UnitPrice = Convert.ToDouble(bookUnitPriceTextBOx.Text);
                anOrder.Advance = Convert.ToDouble(advanceTextBox.Text);
                
                anOrder.SupplyDate = dateTimePicker1.Text;
                
                
                string st = gateway.SaveOrder(anOrder);

                ////InvoiceUI invoice=new InvoiceUI(serialTextBox.Text,customerNameTextBox.Text,customerMobileTextBox.Text,bookNameTextBox.Text,writerNameTextBox.Text,bookEditionTextBox.Text,typeOfBookTextBox.Text,bookPrintTextbox.Text,bookQuantityTextBOx.Text,bookUnitPriceTextBOx.Text,advanceTextBox.Text,dueTextBox.Text,dateTimePicker1.Text);

                //invoice.ShowDialog();
                TempOrder aOrder=new TempOrder();
                aOrder.SerialNo = serialTextBox.Text;
                aOrder.CustomerName = customerNameTextBox.Text;
                aOrder.MobileNo = customerMobileTextBox.Text;
                aOrder.BookName = bookNameTextBox.Text;
                aOrder.WriterName = writerNameTextBox.Text;
                aOrder.Edition = bookEditionTextBox.Text;
                aOrder.Type = typeOfBookTextBox.Text;
                aOrder.Print = bookPrintTextbox.Text;
                aOrder.Quantity = Convert.ToInt16(bookQuantityTextBOx.Text);
                aOrder.Unitprice = Convert.ToDouble(bookUnitPriceTextBOx.Text);
                aOrder.Total =
                    (Convert.ToInt16(bookQuantityTextBOx.Text)*Convert.ToDouble(bookUnitPriceTextBOx.Text));
                aOrder.Advance = Convert.ToDouble(advanceTextBox.Text);
                aOrder.Due = Convert.ToDouble(dueTextBox.Text);

                aOrder.SupplyDate = dateTimePicker1.Text;
                aOrder.MemoNumber = memoNumver;
                TempOrderGateway gateway1=new TempOrderGateway();
                gateway1.SaveTempOrer(aOrder);

                MessageBox.Show(st,"Information",MessageBoxButtons.OK,MessageBoxIcon.Information);

               

                ClearALLtextBox();
                LoadAllBook();

                serialTextBox.Text = LastAddedInvestlNo().ToString();
                CustomerNamteSuggestion();
                AtocompleteTextBox();
                

            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);

                //MessageBox.Show("Please fill every fields properly.", "Error", MessageBoxButtons.OK,
                //        MessageBoxIcon.Error);
            }


        }