// Method to Save Info to DataBase // Includes updating Existing Info private void btn_Save_Click(object sender, EventArgs e) { if (cb_Select.SelectedIndex == -1) // Check for New Broker { BrokerageModel newBroker = new BrokerageModel(); // Gather Information newBroker.BrokerageName = txt_BrokerageName.Text; newBroker.BrokerageAddress = txt_Address.Text; newBroker.AccountNum = txt_AccountNum.Text; newBroker.BrokerName = txt_BrokerName.Text; newBroker.PhoneNum = txt_PhoneNum.Text; newBroker.Email = txt_Email.Text; newBroker.CommissionRate = Convert.ToDecimal(txt_CommissionRate.Text); // Save to Database; GlobalConfig.Connection.Broker_AddNew(newBroker); //Add to list of Brokerages Brokers.Add(newBroker); UpdateSelections(Brokers.Count - 1); SetEdit(false); cb_Select.Visible = true; chk_Edit.Visible = true; btn_Save.Enabled = false; btn_Revert.Enabled = false; } else { // Gather information oldInfo.BrokerageName = txt_BrokerageName.Text; oldInfo.BrokerageAddress = txt_Address.Text; oldInfo.AccountNum = txt_AccountNum.Text; oldInfo.BrokerName = txt_BrokerName.Text; oldInfo.PhoneNum = txt_PhoneNum.Text; oldInfo.Email = txt_Email.Text; oldInfo.CommissionRate = Convert.ToDecimal(txt_CommissionRate.Text); // Update Database GlobalConfig.Connection.Broker_Update(oldInfo); SetEdit(false); cb_Select.Visible = true; chk_Edit.Visible = true; btn_Save.Enabled = false; btn_Revert.Enabled = false; InfoChanged = false; chk_Edit.Checked = false; } }
// Retrieve the Selected Brokers Information private void LoadSelectedBroker() { if (cb_Select.SelectedItem == null) { cb_Select.SelectedIndex = 0; } // Load the Selected Brokerage Information BrokerageModel model = (BrokerageModel)cb_Select.SelectedItem; oldInfo = model; LoadDisplay(model); }
// Update the Form Display private void LoadDisplay(BrokerageModel model) { chk_Edit.Checked = false; txt_BrokerageName.Text = model.BrokerageName; txt_Address.Text = model.BrokerageAddress; txt_AccountNum.Text = model.AccountNum; txt_BrokerName.Text = model.BrokerName; txt_PhoneNum.Text = model.PhoneNum; txt_Email.Text = model.Email; txt_CommissionRate.Text = model.CommissionRate.ToString("N2"); // Set Info Change Flag to False InfoChanged = false; }
// Method to handle New Button Click private void btn_New_Click(object sender, EventArgs e) { if (!InfoChanged) { cb_Select.SelectedIndex = -1; // No Selected Item // Clear all Textboxes foreach (Control mctl in this.Controls) { if (mctl is GroupBox) { foreach (Control ctl in mctl.Controls) { if (ctl is TextBox) { ctl.Text = string.Empty; } } } else if (mctl is TextBox) { mctl.Text = string.Empty; } } // Set all Textboxes to Enabled SetEdit(true); //Hide Select ComboBox and Edit Checkbox cb_Select.Visible = false; chk_Edit.Visible = false; // Rename Revert Button to Cancel btn_Revert.Text = "Cancel"; //Enable Save Button btn_Save.Enabled = true; btn_Revert.Enabled = true; btn_New.Enabled = false; // Set OldInfo to a new instance oldInfo = new BrokerageModel(); } else { MessageBox.Show("You have Changed the Information.\n" + "Please Save or Revert.", "Information Changed"); } }
// Method to Update Brokerage Information public void Broker_Update(BrokerageModel model) { using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db))) { var p = new DynamicParameters(); p.Add("@BrokerId", model.BrokerId); p.Add("@BrokerageName", model.BrokerageName); p.Add("@BrokerageAddress", model.BrokerageAddress); p.Add("@AccountNumber", model.AccountNum); p.Add("@BrokerName", model.BrokerName); p.Add("@PhoneNum", model.PhoneNum); p.Add("@Email", model.Email); p.Add("@CommissionRate", model.CommissionRate); connection.Execute("dbo.sp_Brokerages_Update", p, commandType: CommandType.StoredProcedure); } }
// Method to add a Brokerage to Database public void Broker_AddNew(BrokerageModel model) { using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db))) { var p = new DynamicParameters(); p.Add("@BrokerageName", model.BrokerageName); p.Add("@BrokerageAddress", model.BrokerageAddress); p.Add("@AccountNumber", model.AccountNum); p.Add("@BrokerName", model.BrokerName); p.Add("@PhoneNum", model.PhoneNum); p.Add("@Email", model.Email); p.Add("@CommissionRate", model.CommissionRate); p.Add("@BrokerId", 0, dbType: DbType.Int32, direction: ParameterDirection.Output); connection.Execute("dbo.sp_Brokages_Insert", p, commandType: CommandType.StoredProcedure); model.BrokerId = p.Get <int>("@BrokerId"); } }
// Save Button Click Event Handler private void Btn_Save_Click(object sender, EventArgs e) { //Create Instances of Transaction and Valuation TransactionModel transaction = new TransactionModel(); ValuationModel valuation = new ValuationModel(); decimal shares = 0m; decimal price = Decimal.Parse(tx_TransPrice.Text); broker = (BrokerageModel)cb_Broker.SelectedItem; switch (tType) { case TransactionType.Buy: { shares = GetShares(); //Load Information and Valuation into Correct variables transaction = CreateTransaction(price, shares, broker.BrokerId); valuation = CalculateCurrentValue(price, shares, broker.CommissionRate); //Save Transaction and Valuation to Database GlobalConfig.Connection.Transaction_AddNew(transaction); // Upate Valuations Processor.UpDateValuations(valuation); break; } case TransactionType.Sale: { decimal sharesOwned = Decimal.Parse(lbl_SharesOwned.Text); shares = GetShares(); if (shares > sharesOwned) { //Load Information and Valuation into Correct variables transaction = CreateTransaction(price, shares, broker.BrokerId); valuation = CalculateCurrentValue(price, -shares); //Save Transaction and Valuation to Database GlobalConfig.Connection.Transaction_AddNew(transaction); // CUPdate valuations Processor.UpDateValuations(valuation); break; } else { MessageBox.Show("You Can Not Sale More Shares than you own."); DialogResult = DialogResult.None; break; } } case TransactionType.Update: { //Load Valuation into Correct variable valuation = CalculateCurrentValue(price); //Save Valuation to Database and Update other Stocks Processor.UpDateValuations(valuation); break; } case TransactionType.Split: { // Uses Date, Price, Old Shares, New Shares Decimal oldShares = 0m, newShares = 0m; decimal sharesowned = Decimal.Parse(lbl_SharesOwned.Text); foreach (Control ctl in pnl_Split.Controls) { if (ctl is TextBox) { if (ctl.Name.Contains("Old")) { oldShares = Decimal.Parse(ctl.Text); } else if (ctl.Name.Contains("New")) { newShares = Decimal.Parse(ctl.Text); } } } //Create Transaction transaction = CalcStockSplit(price, newShares, oldShares, sharesowned); transaction.BrokerId = 4; // This should be 0 for stock splits and dividends // Save Transaction GlobalConfig.Connection.Transaction_AddNew(transaction); break; } case TransactionType.Dividend: { broker = GlobalConfig.Connection.Broker_GetAll().FirstOrDefault(); // Uses only Date and Price (Price is Dividend per Share) transaction = CreateTransaction(price, 0, 0); transaction.BrokerId = 4; // Set to Admin Broker Id transaction.Fee = 0M; // Set to NO Broker Fee GlobalConfig.Connection.Transaction_AddNew(transaction); break; } } }