protected void GetProduct()
        {
            try
            {
                //connect to db via EF
                using (spsEntities1 db = new spsEntities1())
                {
                    Int32 SupplementID = Convert.ToInt32(Request.QueryString["SupplementID"]);

                    Supplement s = (from objs in db.Supplements
                                 where objs.SupplementID == SupplementID
                                 select objs).FirstOrDefault();

                    //set the name of the product being ordered for confirmation
                    if (s != null)
                    {
                        lblProductName.Text = s.Name;
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("/Error.aspx?Message=" + ex.Message);
            }
        }
        protected void GetProduct()
        {
            //populate form wih existing category record
            Int32 CategoryID = Convert.ToInt32(Request.QueryString["CategoryID"]);

            try
            {
                //connect to db via EF
                using (spsEntities1 db = new spsEntities1())
                {
                    Category c = (from objc in db.Categories
                                 where objc.CategoryID == CategoryID
                                 select objc).FirstOrDefault();

                    //map the category properties to the form controls
                    txtName.Text = c.Name;
                    txtDescription.Text = c.Description;

                    //map the category properties to the form controls if we found a match
                    if (c != null)
                    {
                        txtName.Text = c.Name;
                        txtDescription.Text = c.Description;
                    }
                }
            }
            catch (Exception)
            {
                Server.Transfer("/Error.aspx");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((!IsPostBack))
            {
                using (spsEntities1 db = new spsEntities1())
                {
                    //get current logged in user id
                    string currentUserId = System.Web.HttpContext.Current.User.Identity.GetUserId();
                    var focus = (from f in db.Focus orderby f.Name select f);

                    //populate focus list box
                    lbxFocus.DataSource = focus.ToList();
                    lbxFocus.DataTextField = "Name";
                    lbxFocus.DataValueField = "FocusID";
                    lbxFocus.DataBind();

                    //get selected user focus
                    var userFocus = (from uf in db.UserFocus orderby uf.Focus.Name
                                     where uf.UserID == currentUserId
                                     select uf.FocusID);

                    //explicitly select the foci that the user has already set
                    foreach (ListItem item in lbxFocus.Items)
                    {
                        foreach (int focusId in userFocus)
                        {
                            if (focusId == int.Parse(item.Value))
                                item.Selected = true;
                        }
                    }
                }
            }
        }
        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 GetProducts()
        {
            //try
            //{
            //connect to EF
            using (spsEntities1 db = new spsEntities1())
            {
                string currentUserId = System.Web.HttpContext.Current.User.Identity.GetUserId();

                var topPurchases = (from tp in db.TopPurchases
                                 select tp);

                var userRecommended = (from ufr in db.UserFocusRecommendations
                                       where ufr.UserID == currentUserId
                                       select ufr);

                //bind the result to the gridview
                grdTopPurchases.DataSource = topPurchases.ToList();
                grdTopPurchases.DataBind();

                grdRecommended.DataSource = userRecommended.ToList();
                grdRecommended.DataBind();
            }
            //}
            //catch (Exception)
            //{
            //    Server.Transfer("/error.aspx");
            //}
        }
        protected void GetOrders()
        {
            try
            {
                //get current user id
                string currentUserId = System.Web.HttpContext.Current.User.Identity.GetUserId();

                //connect to EF
                using (spsEntities1 db = new spsEntities1())
                {
                    string SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString();

                    //select order info for current logged in user
                    var orders = from o in db.Orders
                                 join s in db.Supplements on o.SupplementID equals s.SupplementID
                                 where o.UserID == currentUserId
                                 select new { o.OrderID, s.Name, s.Description };

                    //bind the result to the gridview
                    grdOrders.DataSource = orders.AsQueryable().OrderBy(SortString).ToList();
                    grdOrders.DataBind();
                }
            }
            catch (Exception err)
            {
                Server.Transfer("/Error.aspx");
            }
        }
        protected void GetProducts()
        {
            try
            {
                //connect to EF
                using (spsEntities1 db = new spsEntities1())
                {
                    String SortString = Session["SortColumn"].ToString() + " " + Session["SortDirection"].ToString();

                    //query the students table using EF and LINQ
                    var Products = (from p in db.Supplements
                                   join c in db.Categories on p.CategoryID equals c.CategoryID
                                   where p.Deleted == false
                                   select new { p.SupplementID, p.Name, p.Description, Category = c.Name });

                    //bind the result to the gridview
                    grdProducts.DataSource = Products.AsQueryable().OrderBy(SortString).ToList();
                    grdProducts.DataBind();
                }
            }
            catch (Exception)
            {
                Server.Transfer("/Error.aspx");
            }
        }
        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 GetProduct()
        {
            //populate form wih existing supplement record
            Int32 SupplementID = Convert.ToInt32(Request.QueryString["SupplementID"]);

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

                    //generate a string, and populate with a list of foci related to the current supplement
                    String Foci = "";
                    var supplementFocus = (from sf in db.SupplementFocus
                                           orderby sf.Focus.Name
                                           join f in db.Focus on sf.FocusID equals f.FocusID
                                           where sf.SupplementID == SupplementID
                                           select sf.Focus.Name);
                    //loop through all the foci relate to the supplement
                    foreach (String focusName in supplementFocus)
                    {
                        //add them to the string
                        Foci += focusName + " ";
                    }

                    //map the supplement properties to the form controls
                    lblName.Text = s.Name;
                    lblCategory.Text = s.Category.Name;
                    lblDescription.Text = s.Description;
                    lblFocus.Text = Foci;
                }
            }
            catch (Exception)
            {
                Server.Transfer("/Error.aspx");
            }
        }
        protected void GetProduct()
        {
            //populate form wih existing supplement record
            Int32 SupplementID = Convert.ToInt32(Request.QueryString["SupplementID"]);

            try
            {
                //connect to db via EF
                using (spsEntities1 db = new spsEntities1())
                {
                    //query database and get relevant supplement object
                    Supplement s = (from objs in db.Supplements
                                 where objs.SupplementID == SupplementID
                                 select objs).FirstOrDefault();

                    //map the supplement properties to the form controls
                    txtName.Text = s.Name;
                    lbxCategory.SelectedValue = s.CategoryID.ToString();
                    txtDescription.Text = s.Description;

                    //map the student properties to the form controls if we found a match
                    if (s != null)
                    {
                        txtName.Text = s.Name;
                        lbxCategory.SelectedValue = s.CategoryID.ToString();
                        txtDescription.Text = s.Description;
                    }
                }
            }
            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 Page_Load(object sender, EventArgs e)
        {
            //if save wasnt clicked AND we have a ProductID in the url
            if ((!IsPostBack))
            {
                using (spsEntities1 db = new spsEntities1())
                {
                    //get current supplement id from querystring
                    Int32 SupplementID = Convert.ToInt32(Request.QueryString["SupplementID"]);

                    //query and populate categories in category list box
                    var categories = (from c in db.Categories orderby c.Name select c);
                    lbxCategory.DataSource = categories.ToList();
                    lbxCategory.DataTextField = "Name";
                    lbxCategory.DataValueField = "CategoryID";
                    lbxCategory.DataBind();

                    //query and populate foci into focus list box
                    var focus = (from f in db.Focus orderby f.Name select f);
                    lbxFocus.DataSource = focus.ToList();
                    lbxFocus.DataTextField = "Name";
                    lbxFocus.DataValueField = "FocusID";
                    lbxFocus.DataBind();

                    //get what foci are selected for the given supplement
                    var supplementFocus = (from sf in db.SupplementFocus
                                           orderby sf.Focus.Name
                                           where sf.SupplementID == SupplementID
                                           select sf.FocusID);

                    //explicitly select the foci in the list box to show what is already selected
                    foreach (ListItem item in lbxFocus.Items)
                    {
                        foreach (int focusId in supplementFocus)
                        {
                            if (focusId == int.Parse(item.Value))
                                item.Selected = true;
                        }
                    }
                }
                if (Request.QueryString.Count > 0)
                {
                    GetProduct();
                }
            }
        }
        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");
            }
        }