private void loadStockPortfoliosFromDB() { // connect to DB cn = getSGBDConnection(); if (!verifySGBDConnection()) { return; } // get all employees SqlCommand cmd = new SqlCommand("SELECT * FROM STOCK_PORTFOLIO", cn); SqlDataReader reader = cmd.ExecuteReader(); listBox1.Items.Clear(); // load employees while (reader.Read()) { StockPortfolio stock = new StockPortfolio(); stock.Id = reader["id"].ToString(); stock.Risk = reader["risk"].ToString(); listBox1.Items.Add(stock); } // close connection to BD cn.Close(); //Show first employee currentListEntry = 0; showEntry(); }
private void submitEntry(StockPortfolio stock) { if (!verifySGBDConnection()) { return; } SqlCommand cmd = new SqlCommand(); cmd.CommandText = "INSERT STOCK_PORTFOLIO (id, risk) " + "VALUES (@id, @risk) "; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@id", stock.Id); cmd.Parameters.AddWithValue("@risk", Convert.ToDecimal(stock.Risk)); cmd.Connection = cn; try { cmd.ExecuteNonQuery(); } catch (Exception ex) { throw new Exception("Failed to update contact in database. \n ERROR MESSAGE: \n" + ex.Message); } finally { cn.Close(); } }
/** * Loan Data Handling Helper Functions */ private bool saveStockPortfolio() { StockPortfolio stock = new StockPortfolio(); try { stock.Id = idTxt.Text; stock.Risk = riskTxt.Text; } catch (Exception ex) { MessageBox.Show(ex.Message); return(false); } if (adding) { submitEntry(stock); loadStockPortfoliosFromDB(); } else { updateStockPortfolio(stock); listBox1.Items[currentListEntry] = stock; } return(true); }
/** * Interaction Helper Functions */ // form field's content related private void showEntry() { if (listBox1.Items.Count == 0 | currentListEntry < 0) { return; } StockPortfolio stock = new StockPortfolio(); stock = (StockPortfolio)listBox1.Items[currentListEntry]; idTxt.Text = stock.Id; riskTxt.Text = stock.Risk; }
private void updateStockPortfolio(StockPortfolio stock) { int rows = 0; if (!verifySGBDConnection()) { return; } SqlCommand cmd = new SqlCommand(); cmd.CommandText = "UPDATE STOCK_PORTFOLIO " + "SET risk = @risk " + "WHERE id = @id"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@id", stock.Id); cmd.Parameters.AddWithValue("@risk", Convert.ToDecimal(stock.Risk)); cmd.Connection = cn; try { rows = cmd.ExecuteNonQuery(); } catch (Exception ex) { throw new Exception("Failed to update contact in database. \n ERROR MESSAGE: \n" + ex.Message); } finally { if (rows == 1) { MessageBox.Show("Update OK"); } else { MessageBox.Show("Update NOT OK"); } cn.Close(); } }