protected void Page_Load(object sender, EventArgs e)
 {
     if (!this.IsPostBack)
     {
         var repository = new ExpenseRepository();
         var expenses = repository.GetExpensesByUser(this.User.Identity.Name);
         this.MyExpensesGridView.DataSource = expenses;
         this.DataBind();
     }
 }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string storageId;
                try
                {
                    storageId = this.Request.QueryString["id"];
                }
                catch (ArgumentNullException exception)
                {
                    Log.Write(EventKind.Error, exception.Message);
                    throw;
                }

                var expenseRepository = new ExpenseRepository(TimeSpan.FromMinutes(this.Session.Timeout));
                Expense expense = expenseRepository.GetExpenseById(this.User.Identity.Name, storageId);

                if (expense == null)
                {
                    string errorMessage = string.Format(CultureInfo.CurrentCulture, "There is no expense with the id {0}.", storageId);
                    Log.Write(EventKind.Error, errorMessage);
                    throw new ArgumentException(errorMessage);
                }

                if (expense.UserName != this.User.Identity.Name)
                {
                    string errorMessage = string.Format("{0} cannot access the expense with id {1}.", this.User.Identity.Name, expense.Id);
                    throw new UnauthorizedAccessException(errorMessage);
                }

                this.ExpenseDate.Text = expense.Date.ToString("yyyy-MM-dd");
                this.ExpenseTitle.Text = Server.HtmlEncode(expense.Title);
                this.ExpenseItemsGridView.DataSource = expense.Details;
                this.ExpenseItemsGridView.DataBind();
                this.ExpenseReimbursementMethod.Text = Server.HtmlEncode(Enum.GetName(typeof(ReimbursementMethod), expense.ReimbursementMethod));
                this.ExpenseCostCenter.Text = Server.HtmlEncode(expense.CostCenter);
                this.Approver.Text = Server.HtmlEncode(expense.ApproverName);
            }
        }
        private void SaveExpense()
        {
            var userRepository = new UserRepository();
            var user = userRepository.GetUser(this.User.Identity.Name);

            var approverName = this.Approver.Text;

            if (string.IsNullOrEmpty(approverName))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "The approver {0} does not exists", this.Approver.Text));
            }

            var newExpense = new Expense
            {
                Id = StorageKey.Now.InvertedTicks,
                Title = this.ExpenseTitle.Text,
                CostCenter = user.CostCenter,
                Approved = false,
                ReimbursementMethod = (ReimbursementMethod)Enum.Parse(typeof(ReimbursementMethod), this.ExpenseReimbursementMethod.SelectedItem.Value),
                UserName = user.UserName,
                Date = DateTime.Parse(this.ExpenseDate.Text, CultureInfo.CurrentUICulture),
                ApproverName = approverName
            };
            this.ExpenseItems.ForEach(ei => newExpense.Details.Add(ei));

            var expenseRepository = new ExpenseRepository();
            expenseRepository.SaveExpense(newExpense);

            user.PreferredReimbursementMethod = (ReimbursementMethod)Enum.Parse(typeof(ReimbursementMethod), this.ExpenseReimbursementMethod.SelectedValue);
            userRepository.UpdateUserPreferredReimbursementMethod(user);
        }