protected void Sell(int amount, int companyId) { int userShares; Guid UserId; User user = new User(); Comapny company = new Comapny(); UserId = new Guid(MySession.Current.UserId); try { user = UserDB.GetUser(UserId); company = CompanyDB.GetCompanyShares(companyId); userShares = SharesDB.GetUserShares(UserId, companyId); } catch (SqlException sqlEx) { lblErrorMessage.Text = "A database error has occurred.<br /><br />" + sqlEx.Message; return; } if (userShares - amount >= 0) { user.cash += company.sharePrice * amount; company.shares += amount; userShares -= amount; try { SharesDB.UpdateCash(UserId, user.cash); if (userShares == 0) { SharesDB.DeleteUserShares(UserId, companyId); } else { SharesDB.UpdateUserShares(UserId, companyId, userShares); } CompanyDB.UpdateCompanyShares(companyId, company.shares); dsUserStocks.SelectParameters["UserId"].DefaultValue = MySession.Current.UserId; } catch (SqlException sqlEx) { lblErrorMessage.Text = "A database error has occurred.<br /><br />" + sqlEx.Message; return; } try { UserHistoryDB.InsertHistory(UserId, companyId, amount, 's', 0, company.sharePrice); } catch (SqlException sqlEx) { lblErrorMessage.Text = "A database error has occurred.<br /><br />" + sqlEx.Message; // lblConfirmation.Text = ""; return; } gvwUserStocks.DataBind(); lblErrorMessage.Text = ""; lblConfirmation.Text = "Share sold successfully"; } else { lblConfirmation.Text = ""; lblErrorMessage.Text = "You can not sell that many shares"; } }
protected void Buy(int amount, int companyId) { int userShares; Guid UserId; User user = new User(); Comapny company = new Comapny(); UserId = new Guid(MySession.Current.UserId); try { user = UserDB.GetUser(UserId); company = CompanyDB.GetCompanyShares(companyId); userShares = SharesDB.GetUserShares(UserId, companyId); } catch (SqlException sqlEx) { lblErrorMessage.Text = "A database error has occurred.<br /><br />" + sqlEx.Message; lblConfirmation.Text = "The stock could not be purchased"; return; } //if the user has enough money if (user.cash - company.sharePrice * amount >= 0) { //if the company has enough shares if (company.shares - amount >= 0) { user.cash -= company.sharePrice * amount; company.shares -= amount; try { if (userShares > 0) { SharesDB.UpdateUserShares(UserId, companyId, userShares + amount); } else { SharesDB.InsertUserShares(UserId, companyId, amount, userShares + amount); } SharesDB.UpdateCash(UserId, user.cash); CompanyDB.UpdateCompanyShares(companyId, company.shares); } catch (SqlException sqlEx) { lblErrorMessage.Text = "A database error has occurred.<br /><br />" + sqlEx.Message; // lblConfirmation.Text = ""; return; } try { UserHistoryDB.InsertHistory(UserId, companyId, amount, 'b', company.sharePrice, 0); } catch (SqlException sqlEx) { lblErrorMessage.Text = "A database error has occurred.<br /><br />" + sqlEx.Message; // lblConfirmation.Text = ""; return; } gwBuyStocks.DataBind(); lblErrorMessage.Text = ""; lblConfirmation.Text = "Share bought successfully"; } //else inform the user of the lack of available shares else { lblConfirmation.Text = ""; lblErrorMessage.Text = "Sorry no shares of that company are currently available for purchase"; } } //else call the user poor else { lblErrorMessage.Text = "You are too poor"; lblConfirmation.Text = ""; } }