protected void btnExpense_Click(object sender, EventArgs e) { //Retrive Session variable to get UserID Int32 UserName = (Int32)(Session["UserName"]); try { using (ExpMgmtEntities db = new ExpMgmtEntities()) { //use the Expense model to save the new record Expense ex = new Expense(); Account ac = new Account(); User us = new User(); us = (from objU in db.Users where objU.UserID == UserName select objU).FirstOrDefault(); ex.ExpType = ddlExpense.SelectedValue; ex.ExpAmount = Convert.ToDouble(txtAmount.Text); ex.UserID = UserName; ex.Date = Convert.ToDateTime(DateTime.Today); ex.AccountType = ddlAccountType.SelectedValue; //Reduce expense amount from equivalent User account if (ddlAccountType.SelectedValue == "Credit") { ac = (from objA in db.Accounts where objA.UserID == us.UserID select objA).FirstOrDefault(); ac.CreditBalance = ac.CreditBalance - Convert.ToDouble(txtAmount.Text); } else { ac = (from objA in db.Accounts where objA.UserID == us.UserID select objA).FirstOrDefault(); ac.DebitBalance = ac.DebitBalance - Convert.ToDouble(txtAmount.Text); } //run the update or insert db.Expenses.Add(ex); db.SaveChanges(); //redirect to the updated students page Response.Redirect("/user/expense.aspx"); } } catch { Response.Redirect("/errors.aspx"); } }
private void getExps(string startdate, string enddate, string acctype) { try { Int32 Uid = (Int32)(Session["UserName"]); using (ExpMgmtEntities db = new ExpMgmtEntities()) { Expense ex = new Expense(); if ((startdate != "") && (enddate != "") && (acctype != "")) { DateTime sd = Convert.ToDateTime(startdate); DateTime ed = Convert.ToDateTime(enddate); string at = ddlAccountType.SelectedValue; var Exp = from e in db.Expenses where e.UserID == Uid && e.Date >= sd && e.Date <= ed && e.AccountType == at select new { e.ExpID, e.ExpType, e.Date, e.ExpAmount, e.AccountType }; if (Exp != null) { grdExpense.DataSource = Exp.ToList(); grdExpense.DataBind(); } else { Response.Redirect("/user/expense.aspx"); } } else { var Exp = from e in db.Expenses where e.UserID == Uid select new { e.ExpID, e.ExpType, e.Date, e.ExpAmount, e.AccountType }; if (Exp != null) { grdExpense.DataSource = Exp.ToList(); grdExpense.DataBind(); } else { Response.Redirect("/user/expense.aspx"); } } } } catch { Response.Redirect("/errors.aspx"); } }