public void SaveSelfOrder(SelfOrder selfOrder)
        {

            
        

            if (HasThisBookalradyinList(selfOrder.BName,selfOrder.BWriter,selfOrder.BEdition) == false)
            {
                DBManager manager = new DBManager();
                SqlConnection connection = manager.Connection();
                connection.Close();
                string selectQuery = "Insert into Self_Order values('" + selfOrder.BName + "','" +
                                     selfOrder.BWriter + "','" + selfOrder.BEdition + "','" + selfOrder.Quantity +
                                     "','" + DateTime.Now.ToShortDateString() + "')";

                SqlCommand cmd = new SqlCommand(selectQuery, connection);
                connection.Open();
                int i = cmd.ExecuteNonQuery();
                MessageBox.Show("Saved", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);


                
            }

            else
            {
                MessageBox.Show("This book already include.","Message",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }

        }
        private bool HasThisBookalradyinList(string p,string st1,string st2)
        {
            List<SelfOrder> orders = new List<SelfOrder>();
            {
                string b_Name, b_Edition, b_Writer;


                DBManager manager = new DBManager();
                SqlConnection connection = manager.Connection();

                string query = "Select [Book Name],[Writer Name],Edition from Self_Order";
                SqlCommand command = new SqlCommand(query, connection);
                connection.Open();
                SqlDataReader reader1 = command.ExecuteReader();
                
                while (reader1.Read())
                {
                    b_Name = reader1[0].ToString();
                    b_Writer = reader1[1].ToString();
                    b_Edition = reader1[2].ToString();

                    SelfOrder anOrder = new SelfOrder();
                    anOrder.BName = b_Name;
                    anOrder.BWriter = b_Writer;
                    anOrder.BEdition = b_Edition;
                    orders.Add(anOrder);
                }
            }




            foreach (SelfOrder aOd in orders)
            {
                if (aOd.BName.Equals(p) && aOd.BWriter.Equals(st1)&& aOd.BEdition.Equals(st2))
                {
                    return true;
                }
            }
            return false;
        }
        public List<SelfOrder> GetAll()
        {
            DBManager manager=new DBManager();
            SqlConnection connection = manager.Connection();
            connection.Open();
            List<SelfOrder> orders = new List<SelfOrder>();
            try
            {
                string query = "select * from Self_Order";
                SqlCommand command = new SqlCommand(query, connection);
              
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    SelfOrder anOrder = new SelfOrder();
                    anOrder.SerilNo = reader[0].ToString();
                    anOrder.BName = reader[1].ToString();
                    anOrder.BWriter = reader[2].ToString();
                    anOrder.BEdition = reader[3].ToString();
                    anOrder.BType = reader[4].ToString();
                    anOrder.BPrint = reader[5].ToString();
                    anOrder.Quantity = Convert.ToInt16(reader[6]);
                    anOrder.MemoNo = Convert.ToInt16(reader[8]);
                    orders.Add(anOrder);
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    ;
            }
            finally
            {
                connection.Close();
            }

            return orders;
        }
        private void SaveSelfOrder(object sender, EventArgs e)
        {
            try
            {
                SelfOrder selfOrder = new SelfOrder();
                selfOrder.BName = booknameTextbox.Text;
                selfOrder.BWriter = writernameTextbox.Text;
                selfOrder.BEdition = editionTextbox.Text;
                selfOrder.Quantity = Convert.ToInt16(quantityTextbox.Text);
                if (selfOrder.BName.Equals(""))
                {
                    MessageBox.Show("Enter the Book name.","Message",MessageBoxButtons.OK,MessageBoxIcon.Warning);

                }
                else if(selfOrder.BWriter.Equals(""))
                {
                    MessageBox.Show("Enter the writer name.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);  
                }
                else if (selfOrder.BEdition.Equals(""))
                {
                    MessageBox.Show("Enter the book edition.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SelfOrderGateway gateway = new SelfOrderGateway();

                    gateway.SaveSelfOrder(selfOrder);
                }
                
            }
            catch (FormatException)
            {
                
                
            }


        }
        private void button1_Click(object sender, EventArgs e)
        {
            //string name = "N''জাভা";
            //    string writer = "N''বালাগুরুশামী";
            //    string edition = "3rd";

                DBManager manager = new DBManager();
                SqlConnection connection = manager.Connection();
            try
            {


                string selectQuery = "SELECT * FROM Self_Order where [Book Name]=@name and [Writer Name]=@writer and Edition=@edition";
                connection.Open();

                SqlCommand command = new SqlCommand(selectQuery, connection);
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@name", textBox1.Text);
                command.Parameters.AddWithValue("@writer", writer_textBox.Text);
                command.Parameters.AddWithValue("@edition", edition_textBox.Text);
                SqlDataReader reader = command.ExecuteReader();
                List<SelfOrder> bookList=new List<SelfOrder>();
                while (reader.Read())
                {
                    string id = reader[0].ToString();
                    string bookname = reader[1].ToString();
                    string writer = reader[2].ToString();
                    string edition = reader[3].ToString();
                    string type = reader[4].ToString();
                    string print = reader[5].ToString();
                    string quantity = reader[6].ToString();
                   SelfOrder anOrder=new SelfOrder();
                    anOrder.SerilNo = id;
                    anOrder.BName = bookname;
                    anOrder.BWriter = writer;
                    anOrder.BEdition= edition;
                    anOrder.BType = type;
                    anOrder.BPrint = print;
                    if (quantity.Equals("")||quantity.Equals("0"))
                    {
                        anOrder.Quantity =0;  
                    }
                    else
                    {
                        anOrder.Quantity = Convert.ToInt16(quantity);
                    }
                    
                   bookList.Add(anOrder);
                }
                //dataGridView1.Columns[0].DataPropertyName = id;
                //dataGridView1.AutoGenerateColumns = false;
                //dataGridView1.Columns[1].DataPropertyName = "Name";
                //dataGridView1.Columns[2].DataPropertyName = "Writer";
                //dataGridView1.Columns[3].DataPropertyName = "Editon";
                dataGridView1.DataSource = bookList;


            }
            catch ( Exception exception)
            {
                MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
            connection.Close();
            }
        }
        private void SaveSelfOrder(object sender, EventArgs e)
        {
            try
            {
                SelfOrder selfOrder = new SelfOrder();
                selfOrder.BName = booknameTextbox.Text;
                selfOrder.BWriter = writernameTextbox.Text;
                selfOrder.BEdition = editionTextbox.Text;
                selfOrder.BType = typeTextBox.Text;
                selfOrder.BPrint = printTexBox.Text;
                selfOrder.Quantity = Convert.ToInt16(quantityTextbox.Text);
                selfOrder.MemoNo = memoNumver;
                if (selfOrder.BName.Equals(""))
                {
                    MessageBox.Show("Enter the Book name.","Message",MessageBoxButtons.OK,MessageBoxIcon.Warning);

                }
                else if(selfOrder.BWriter.Equals(""))
                {
                    MessageBox.Show("Enter the writer name.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);  
                }
                else if (selfOrder.BEdition.Equals(""))
                {
                    MessageBox.Show("Enter the book edition.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SelfOrderGateway gateway = new SelfOrderGateway();

                    gateway.SaveSelfOrder(selfOrder);
                }
                
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
                
            }


        }
        private bool HasThisBookalradyinList(string p,string st1,string st2,string st3,string st4)
        {
            try
            {
                List<SelfOrder> orders = new List<SelfOrder>();
                {
                    string b_Name, b_Edition, b_Writer, b_type, b_print;


                    DBManager manager = new DBManager();
                    SqlConnection connection = manager.Connection();

                    string query = "Select [Book Name],[Writer Name],Edition,Type,[Print] from Self_Order where [Order Date]='" + DateTime.Now.ToShortDateString() + "'";
                    SqlCommand command = new SqlCommand(query, connection);
                    connection.Open();
                    SqlDataReader reader1 = command.ExecuteReader();

                    while (reader1.Read())
                    {
                        b_Name = reader1[0].ToString();
                        b_Writer = reader1[1].ToString();
                        b_Edition = reader1[2].ToString();
                        b_type = reader1[3].ToString();
                        b_print = reader1[4].ToString();
                        SelfOrder anOrder = new SelfOrder();
                        anOrder.BName = b_Name;
                        anOrder.BWriter = b_Writer;
                        anOrder.BEdition = b_Edition;
                        anOrder.BType = b_type;
                        anOrder.BPrint = b_print;
                        orders.Add(anOrder);
                    }
                }




                foreach (SelfOrder aOd in orders)
                {
                    if (aOd.BName.Equals(p) && aOd.BWriter.Equals(st1) && aOd.BEdition.Equals(st2) && aOd.BType.Equals(st3) && aOd.BPrint.Equals(st4))
                    {
                        return true;
                    }
                }
            }
            catch ( Exception exception)
            {
                
               
               MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return false;
        }
        public void SaveSelfOrder(SelfOrder selfOrder)
        {

            try
            {
                //start

                SqlConnection connection1 = manager.Connection();
                connection1.Close();
                string selectQuery1 = "Insert into Current_Self_Order values(@name,@writer,@edition,@type,@print,@quantity,@date)";


                SqlCommand cmd1 = new SqlCommand(selectQuery1, connection1);
                connection1.Open();
                cmd1.Parameters.Clear();
                cmd1.Parameters.AddWithValue("@name", selfOrder.BName);
                cmd1.Parameters.AddWithValue("@writer", selfOrder.BWriter);
                cmd1.Parameters.AddWithValue("@edition", selfOrder.BEdition);
                cmd1.Parameters.AddWithValue("@type", selfOrder.BType);
                cmd1.Parameters.AddWithValue("@print", selfOrder.BPrint);
                cmd1.Parameters.AddWithValue("@quantity", selfOrder.Quantity);
                cmd1.Parameters.AddWithValue("@date", DateTime.Now.ToShortDateString());

                int ix = cmd1.ExecuteNonQuery();
                connection1.Close();
                //end



                if (HasThisBookalradyinList(selfOrder.BName, selfOrder.BWriter, selfOrder.BEdition, selfOrder.BType, selfOrder.BPrint) == false)
                {
                    manager = new DBManager();
                    SqlConnection connection = manager.Connection();
                    connection.Close();
                    string selectQuery = "Insert into Self_Order values(@name,@writer,@edition,@type,@print,@quantity,@date,@memoNumber)";


                    SqlCommand cmd = new SqlCommand(selectQuery, connection);
                    connection.Open();
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@name", selfOrder.BName);
                    cmd.Parameters.AddWithValue("@writer", selfOrder.BWriter);
                    cmd.Parameters.AddWithValue("@edition", selfOrder.BEdition);
                    cmd.Parameters.AddWithValue("@type", selfOrder.BType);
                    cmd.Parameters.AddWithValue("@print", selfOrder.BPrint);
                    cmd.Parameters.AddWithValue("@quantity", selfOrder.Quantity);
                    cmd.Parameters.AddWithValue("@date", DateTime.Now.ToShortDateString());
                    cmd.Parameters.AddWithValue("@memoNumber", selfOrder.MemoNo);
                    int i = cmd.ExecuteNonQuery();
                    MessageBox.Show("Saved", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);



                }

                else
                {
                    MessageBox.Show("This book already include.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception exception)
            {
                
                
               MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }