protected void btnConfirmOrder_Click(object sender, EventArgs e)
        {
            try
            {
                using (spsEntities1 db = new spsEntities1())
                {
                     if (Request.QueryString["SupplementID"] != null)
                     {
                         //get user id of current user
                         string currentUserId = System.Web.HttpContext.Current.User.Identity.GetUserId();
                         int SupplementID = int.Parse(Request.QueryString["SupplementID"]);

                         //new order object to work with
                         Order order = new Order();

                         //set variables in the new order object
                         order.SupplementID = SupplementID;
                         order.UserID = currentUserId;

                         //add order to orders db
                         db.Orders.Add(order);
                         db.SaveChanges();

                         Response.Redirect("/admin/orders.aspx", false);
                     }
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/Error.aspx?Message=" + ex.Message);
            }
        }
        protected void btnUpdateProfile_Click(object sender, EventArgs e)
        {
            try
            {
                //use EG to connect to SQL Server
                using (spsEntities1 db = new spsEntities1())
                {
                    //use the Student model to save the new record
                    AspNetUser user = new AspNetUser();
                    string currentUserId = System.Web.HttpContext.Current.User.Identity.GetUserId();

                    //check the querystring for an id so we can determine add/update
                    if (currentUserId != null)
                    {
                        //get the user object
                        user = (from u in db.AspNetUsers
                             where u.Id == currentUserId
                             select u).FirstOrDefault();
                    }

                    foreach (UserFocus uf in user.UserFocus.ToList())
                    {
                        db.UserFocus.Remove(uf);
                    }

                    foreach (ListItem item in lbxFocus.Items)
                    {
                        if (item.Selected)
                        {
                            UserFocus focus = new UserFocus();
                            focus.FocusID = int.Parse(item.Value);

                            user.UserFocus.Add(focus);
                        }
                    }

                    //call add only if we have no current user ID
                    //if (currentUserId == 0)
                    //{
                    //    db.Supplements.Add(s);
                    //}

                    //run the update or insert
                    db.SaveChanges();

                    //redirect
                    Response.Redirect("/admin/products.aspx");
                }
            }
            catch (Exception)
            {
                Server.Transfer("/error.aspx");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //use EG to connect to SQL Server
                using (spsEntities1 db = new spsEntities1())
                {
                    //use the Category model to save the new record
                    Category c = new Category();
                    Int32 CategoryID = 0;

                    //check the querystring for an id so we can determine add/update
                    if (Request.QueryString["CategoryID"] != null)
                    {
                        //get the id from the url
                        CategoryID = Convert.ToInt32(Request.QueryString["CategoryID"]);

                        //get the current category from EF
                        c = (from objc in db.Categories
                             where objc.CategoryID == CategoryID
                             select objc).FirstOrDefault();
                    }

                    c.Name = txtName.Text;
                    c.Description = txtDescription.Text;

                    //call add only if we have no category ID
                    if (CategoryID == 0)
                    {
                        db.Categories.Add(c);
                    }

                    //run the update or insert
                    db.SaveChanges();

                    //redirect
                    Response.Redirect("/admin/products.aspx");
                }
            }
            catch (Exception)
            {
                Server.Transfer("/error.aspx");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //use EG to connect to SQL Server
                using (spsEntities1 db = new spsEntities1())
                {
                    //use the supplement model to save the new record
                    Supplement s = new Supplement();
                    Int32 SupplementID = 0;

                    //check the querystring for an id so we can determine add/update
                    if (Request.QueryString["SupplementID"] != null)
                    {
                        //get the id from the url
                        SupplementID = Convert.ToInt32(Request.QueryString["SupplementID"]);

                        //get the current student from EF
                        s = (from objs in db.Supplements
                             where objs.SupplementID == SupplementID
                             select objs).FirstOrDefault();
                    }

                    //remove supplement foci relationship(s)
                    foreach (SupplementFocus sf in s.SupplementFocus.ToList())
                    {
                        db.SupplementFocus.Remove(sf);
                    }

                    //add selected values to the supplement focus relationships for the user
                    foreach (ListItem item in lbxFocus.Items)
                    {
                        if (item.Selected)
                        {
                            SupplementFocus focus = new SupplementFocus();
                            focus.FocusID = int.Parse(item.Value);

                            s.SupplementFocus.Add(focus);
                        }
                    }

                    s.Name = txtName.Text;
                    s.Description = txtDescription.Text;
                    s.CategoryID = int.Parse(lbxCategory.SelectedValue);

                    //call add only if we have no supplement ID
                    if (SupplementID == 0)
                    {
                        db.Supplements.Add(s);
                    }

                    //run the update or insert
                    db.SaveChanges();

                    //redirect
                    Response.Redirect("/admin/products.aspx");
                }
            }
            catch (Exception)
            {
                Server.Transfer("/error.aspx");
            }
        }
        protected void grdOrders_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //store which row was clicked
            Int32 selectedRow = e.RowIndex;

            //get the selected StudentID using the grid's Data Key collection
            Int32 OrderID = Convert.ToInt32(grdOrders.DataKeys[selectedRow].Values["OrderID"]);

            try
            {
                using (spsEntities1 db = new spsEntities1())
                {
                    Order order = (from o in db.Orders
                                 where o.OrderID == OrderID
                                 select o).FirstOrDefault();

                    //do the delete
                    db.Orders.Remove(order);
                    db.SaveChanges();
                }

                //refresh the grid
                GetOrders();
            }
            catch (Exception err)
            {
                Server.Transfer("/Error.aspx");
            }
        }
        protected void grdProducts_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //store which row was clicked
            Int32 selectedRow = e.RowIndex;

            //get the selected supplement id using the grid's Data Key collection
            Int32 SupplementID = Convert.ToInt32(grdProducts.DataKeys[selectedRow].Values["SupplementID"]);

            try
            {
                //use EF to remove the selected supplement from the db
                using (spsEntities1 db = new spsEntities1())
                {
                    Supplement s = (from objs in db.Supplements
                                 where objs.SupplementID == SupplementID
                                 select objs).FirstOrDefault();

                    //do the delete (flag as deleted.... because of foreign key issues).
                    s.Deleted = true;
                    db.SaveChanges();
                }

                //refresh the grid
                GetProducts();
            }
            catch (Exception err)
            {
                Server.Transfer("/error.aspx");
            }
        }