public int  LastAddedInvestlNo()
        {
            int a = lastAdded;
            InvestmentGateway inGateway = new InvestmentGateway();
            Invest invest = new Invest();
            
            DBManager manager = new DBManager();
            SqlConnection connection = manager.Connection();
           
            string selectQuery = "SELECT Id From InvestmentCost";
            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.Equals(0))
             {
                 return 0;
             }
             else
             {
                 int x = totalId.Max();
                 return x;
             }

           
        }
        private void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {

                Invest anInvest = new Invest();
                anInvest.Id = Convert.ToInt16(idTextBox.Text);
                anInvest.Book = Convert.ToDouble(bookTextBox.Text);
                anInvest.Paper = Convert.ToDouble(paperTextBox.Text);
                anInvest.Ink = Convert.ToDouble(inkTextBOx.Text);
                anInvest.Equipment = Convert.ToDouble(equipmentTextBox.Text);
                anInvest.Others = Convert.ToDouble(othersTextBox.Text);
                anInvest.Date = dateTimePicker1.Text;
                lastAdded = Convert.ToInt16(idTextBox.Text);

                InvestmentGateway gateway = new InvestmentGateway();
               string status = gateway.SaveTotalInvestment(anInvest);
                MessageBox.Show(status,"Status",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    LastAddedInvestlNo();
                DBManager manager = new DBManager();
                SqlConnection connection = manager.Connection();
                string updateQuery = "UPDATE FInvestmentCost set FBook='" + bookTextBox.Text + "',FPaper='" + paperTextBox.Text + "',FInk='" + inkTextBOx.Text + "',FEquipment='" + equipmentTextBox.Text + "',FOthers='" + othersTextBox.Text + "',FInvestment_Date='" + dateTimePicker1.Text + "' WHERE FId='" + 1 + "'";
                SqlCommand updateCmd = new SqlCommand(updateQuery, connection);
                connection.Open();
                int x = updateCmd.ExecuteNonQuery();
                connection.Close();
                string query = "INSert INTo InvestmentCost values('" + bookTextBox.Text + "','" + paperTextBox.Text + "','" + inkTextBOx.Text + "','" + equipmentTextBox.Text + "','" + othersTextBox.Text + "','" + dateTimePicker1.Text + "')";
                SqlCommand command3=new SqlCommand(query,connection);
                connection.Open();
                int y = command3.ExecuteNonQuery();
                last = LastAddedInvestlNo();
                idTextBox.Text = last.ToString();  

                connection.Close();
                ClearAlltextBox();
            }
            catch (FormatException)
            {
                
                MessageBox.Show("Please fill every field properly.","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
           
        }
        private void ShowAllButton(object sender, EventArgs e)
        {

            InvestmentGateway inGateway = new InvestmentGateway();
            Invest invest = new Invest();
            double total;
            DBManager manager = new DBManager();
            SqlConnection connection = manager.Connection();

            string selectQuery = "SELECT Sum(Book) ,sum(Paper),sum(Ink),sum(Equipment),sum(Others) From InvestmentCost";
            SqlCommand cmd = new SqlCommand(selectQuery, connection);
            connection.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                string a = reader[0].ToString();
                string b = reader[1].ToString();
                string c = reader[2].ToString();
                string d = reader[3].ToString();
                string y = reader[4].ToString();


                if (a.Equals("")&&b.Equals("")&&c.Equals("")&&d.Equals("")&&y.Equals(""))
                {
                    MessageBox.Show("NO data found", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
               
                else
                {

                    showBookCostTextBox.Text = reader[0].ToString();
                    showpaperCostTextBox.Text = reader[1].ToString();
                    showInkCostTextBox.Text = reader[2].ToString();
                    showEquipmentCostTextBox.Text = reader[3].ToString();
                    showOthersCostTextBox.Text = reader[4].ToString();

                    total = (Convert.ToDouble(reader[0]) + Convert.ToDouble(reader[1]) + Convert.ToDouble(reader[2]) + Convert.ToDouble(reader[3]) + Convert.ToDouble(reader[4]));
                    showToataCostTextBox.Text = total.ToString();
                }

            }
            


            
        }