protected void btnRegister_Click(object sender, EventArgs e) { // Create variables to store user information, awaiting creation var userStore = new UserStore<IdentityUser>(); var manager = new UserManager<IdentityUser>(userStore); //creating new user var user = new IdentityUser() { UserName = txtUsername.Text }; IdentityResult result = manager.Create(user, txtPassword.Text); if (result.Succeeded) { //create new user var authenticationManager = HttpContext.Current.GetOwinContext().Authentication; var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie); authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity); //Create new entry on budget table linked to user using (FinalEntities conn = new FinalEntities()) { User newUser = new User(); AspNetUser info = (from u in conn.AspNetUsers where u.UserName == txtUsername.Text select u).FirstOrDefault(); //default values for user newUser.UserID = info.Id; newUser.Name = "UPDATE ACCOUNT INFO"; newUser.Budget = Convert.ToDecimal(0.00); //add new user to database conn.Users.Add(newUser); conn.SaveChanges(); } Response.Redirect("/budget/index.aspx"); } else { //display error in creating user lblStatus.Text = result.Errors.FirstOrDefault(); lblStatus.CssClass = "label label-danger"; } }
protected void btnSave_Click(object sender, EventArgs e) { using (FinalEntities conn = new FinalEntities()) { //prepare to store new information var ex = new Expens(); var userId = User.Identity.GetUserId(); //pull user info var user = (from u in conn.Users where u.UserID == userId select u).FirstOrDefault(); //pull expense information if needed if (!String.IsNullOrEmpty(Request.QueryString["ID"])) { Int32 ExpenseID = Convert.ToInt32(Request.QueryString["ID"]); ex = (from expen in conn.Expenses where expen.Id == ExpenseID select expen).FirstOrDefault(); } //set new expense information ex.Name = txtName.Text; ex.Description = txtDescription.Text; ex.Cost = Convert.ToDecimal(txtCost.Text); ex.UserID = user.Id; //if new expense, add it to database if (String.IsNullOrEmpty(Request.QueryString["ID"])) { conn.Expenses.Add(ex); } //save changes, and redirect to expense table conn.SaveChanges(); Response.Redirect("index.aspx"); } }
//save information protected void btnSave_Click(object sender, EventArgs e) { using (FinalEntities conn = new FinalEntities()) { //get current user id var userId = User.Identity.GetUserId(); //pull information from database var account = (from acc in conn.Users where acc.UserID == userId select acc).FirstOrDefault(); //set new information account.Name = txtAccountName.Text; account.Budget = Convert.ToDecimal(txtBudget.Text); //save changes conn.SaveChanges(); //redirect to account information Response.Redirect("index.aspx"); } }
protected void grdExpenses_RowDeleting(object sender, GridViewDeleteEventArgs e) { //get specified expense using (FinalEntities conn = new FinalEntities()) { Int32 ExpenseID = Convert.ToInt32(grdExpenses.DataKeys[e.RowIndex].Values["ID"]); var exp = (from ex in conn.Expenses where ex.Id == ExpenseID select ex).FirstOrDefault(); //remove expense from database conn.Expenses.Remove(exp); conn.SaveChanges(); } }