protected void getExpenses()
        {
            using (FinalEntities conn = new FinalEntities())
            {
                //get user id
                var userId = User.Identity.GetUserId();

                //initialize expense total
                decimal expenses = 0;

                //get user and expense info
                var user = (from u in conn.Users
                            where u.UserID == userId
                            select u).FirstOrDefault();

                var exps = (from e in conn.Expenses
                            join u in conn.Users on e.UserID equals u.Id
                            where e.UserID == user.Id
                            select e);

                //set budget
                var budget = user.Budget;

                //sort entries
                String Sort = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString();

                //display expenses in table
                grdExpenses.DataSource = exps.AsQueryable().OrderBy(Sort).ToList();
                grdExpenses.DataBind();

                //display budget
                lblBudgetStart.Text ="$" + Convert.ToString(user.Budget);
                lblBudgetMid.Text ="$" + Convert.ToString(user.Budget);

                //if there are expenses...
                if (exps.Count() > 0)
                {
                    //loop through expenses and total them up
                    foreach (Expens expense in exps)
                    {
                        expenses += expense.Cost;
                    }
                    //display expenses and final total
                    lblExpenses.Text = "$" + Convert.ToString(Math.Round(expenses, 2));
                    decimal final = Convert.ToDecimal(budget) - expenses;
                    lblFinal.Text = "$" + Convert.ToString(final);

                    //change color of final total depending on value
                    if (final <= 0)
                    {
                        budgetFinal.Attributes["style"] = "background: red;";
                    }
                    else
                    {
                        budgetFinal.Attributes["style"] = "background: green;";
                    }
                }

            }
        }
        protected void getAccountInfo()
        {
            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();
                //populate editing form
                txtAccountName.Text = account.Name;
                txtBudget.Text = Convert.ToString(account.Budget);
            }
        }
        protected void getExpense()
        {
            //convert expense ID from URL
            Int32 ExpenseID = Convert.ToInt32(Request.QueryString["ID"]);

            using (FinalEntities conn = new FinalEntities())
            {
                //pull expense information
                var exp = (from e in conn.Expenses
                           where e.Id == ExpenseID
                           select e).FirstOrDefault();

                //populate expense form
                txtName.Text = exp.Name;
                txtDescription.Text = exp.Description;
                txtCost.Text = Convert.ToString(exp.Cost);
            }
        }
        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();
            }
        }