protected void GetExercise()
        {
            //using entity framework to connect and get the list of food.
            using (DefaultConnection1 db = new DefaultConnection1())
            {
                Int32 user_id = Convert.ToInt32(Session["user_id"]);
                var ex = from x in db.exercises
                         where x.user_id == user_id
                         select x;

                // bind the ex query result to the grid
                grdExercise.DataSource = ex.ToList();
                grdExercise.DataBind();
            }
        }
 protected void GetExercise()
 {
     //look up the selected exercise and fill the exercise edit form.
     using (DefaultConnection1 db = new DefaultConnection1())
     {
         //Store the ID from the URL in the variable.
         Int32 exercise_id = Convert.ToInt32(Request.QueryString["exercise_id"]);
         //Look up the food
         exercise ex = (from x in db.exercises
                        where x.exercise_id == exercise_id
                        select x).FirstOrDefault();
         //pre populate the form feilds
         txtExerciseName.Text = ex.exercise_name;
         txtReps.Text = ex.exercise_rep;
         txtExerciseDate.Text = Convert.ToString(ex.exercise_date);
     }
 }
        protected void btnAddFood_Click(object sender, EventArgs e)
        {
            //connect
            using (DefaultConnection1 db = new DefaultConnection1())
            {

                //create a new food in memory
                food fd = new food();

                Int32 food_id = 0;

                //check for a url
                if (!String.IsNullOrEmpty(Request.QueryString["food_id"]))
                {
                    //get the id from the url
                    food_id = Convert.ToInt32(Request.QueryString["food_id"]);

                    //look up the food
                    fd = (from f in db.foods
                          where f.food_id == food_id
                          select f).FirstOrDefault();
                }

                //importing the user_id from the session object.
                Int32 user_id = Convert.ToInt32(Session["user_id"]);

                //fill the properties of the new food entry
                fd.food_name = txtFoodName.Text;
                fd.food_calories = Convert.ToInt32(txtCalories.Text);
                fd.food_date = Convert.ToDateTime(txtDate.Text);
                fd.user_id = user_id;

                if (food_id == 0)
                {
                    db.foods.Add(fd);
                }

                //Save the food to the database.
                db.SaveChanges();

                //redierect to the food list page.
                Response.Redirect("food_tracker.aspx");

            }
        }
        protected void grdExercise_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //Identify the excercise ID to be deleted from the row that the user selected from
            Int32 exercise_id = Convert.ToInt32(grdExercise.DataKeys[e.RowIndex].Values["exercise_id"]);

            //Connect
            using (DefaultConnection1 db = new DefaultConnection1())
            {
                exercise ex = (from x in db.exercises
                               where x.exercise_id == exercise_id
                               select x).FirstOrDefault();
                //delete
                db.exercises.Remove(ex);
                db.SaveChanges();

                //refresh
                GetExercise();
            }
        }
        protected void grdFood_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //indentify the food ID to be deleted from the row the user seleted from.
            Int32 food_id = Convert.ToInt32(grdFood.DataKeys[e.RowIndex].Values["food_id"]);

            //connect
            using (DefaultConnection1 db = new DefaultConnection1())
            {
                food fd = (from f in db.foods
                           where f.food_id == food_id
                           select f).FirstOrDefault();

                //delete
                db.foods.Remove(fd);
                db.SaveChanges();

                //refresh
                GetFood();
            }
        }
        protected void GetFood()
        {
            //using entity framework to connect and get the list of food.
            using (DefaultConnection1 db = new DefaultConnection1())
            {
                //old query that shows all the food in the database.
                //var fd = from f in db.foods
                //         select f;

                //new query filtered for logged in user only.
                Int32 user_id = Convert.ToInt32(Session["user_id"]);
                var fd = from f in db.foods
                         where f.user_id == user_id
                         select f;

                //bind the fd query result to the grid
                grdFood.DataSource = fd.ToList();
                grdFood.DataBind();
            }
        }
        protected void GetFood()
        {
            //look up the seleted food and fill the edit form.
            using (DefaultConnection1 db = new DefaultConnection1())
            {
                //store the id from the URL in a variable.
                Int32 food_id = Convert.ToInt32(Request.QueryString["food_id"]);

                //look up the food
                food fd = (from f in db.foods
                           where f.food_id == food_id
                           select f).FirstOrDefault();

                // pre populate the form fields
                txtFoodName.Text = fd.food_name;
                txtCalories.Text =  Convert.ToString(fd.food_calories);
                txtDate.Text = Convert.ToString(fd.food_date);

            }
        }
        protected void btnAddExercise_Click(object sender, EventArgs e)
        {
            //Connect
            using (DefaultConnection1 db = new DefaultConnection1())
            {
                //create new exercise in memory
                exercise ex = new exercise();

                Int32 exercise_id = 0;

                //check for URL
                if (!String.IsNullOrEmpty(Request.QueryString["exercise_id"])){

                    //get the id from the url
                    exercise_id = Convert.ToInt32(Request.QueryString["exercise_id"]);

                    //Look up the food
                    ex = (from x in db.exercises
                          where x.exercise_id == exercise_id
                          select x).FirstOrDefault();
                }

                //importing the user_id from the session object.
                Int32 user_id = Convert.ToInt32(Session["user_id"]);

                //fill the properties of the new Exercise
                ex.exercise_name = txtExerciseName.Text;
                ex.exercise_rep = txtReps.Text;
                ex.exercise_date = Convert.ToDateTime(txtExerciseDate.Text);
                ex.user_id = user_id;

                if(exercise_id == 0){
                    db.exercises.Add(ex);
                }
                //Save the exercise to the data base
                db.SaveChanges();
                //redirect to the exercise tracker page
                Response.Redirect("exercise_tracker.aspx");

            }
        }
        protected void btnSignUp_Click(object sender, EventArgs e)
        {
            //connect
            using (DefaultConnection1 db = new DefaultConnection1())
            {
                //create a new user
                user objI = new user();

                //fill the properties from the form inputs
                objI.first_name = txtFName.Text;
                objI.last_name = txtLName.Text;
                objI.email = txtEmail.Text;

                //salt and hash the plan text password.
                String password = txtPassword.Text;
                String salt = CreateSalt(8);
                String pass_and_salt = password + salt;

                // Create a new instance of the hash crypto service provider.
                HashAlgorithm hashAlg = new SHA256CryptoServiceProvider();

                // Convert the data to hash to an array of Bytes.
                byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(pass_and_salt);

                // Compute the Hash. This returns an array of Bytes.
                byte[] bytHash = hashAlg.ComputeHash(bytValue);

                // Optionally, represent the hash value as a base64-encoded string,
                // For example, if you need to display the value or transmit it over a network.
                string base64 = Convert.ToBase64String(bytHash);

                objI.password = base64;
                objI.salt = salt;

                //save
                db.users.Add(objI);
                db.SaveChanges();

            }
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //connect
            using (DefaultConnection1 db = new DefaultConnection1())
            {
                //create user object in memory
                user objI = new user();

                //first get the salt value for this username
                String email = txtEmailLogin.Text;

                objI = (from em in db.users
                        where em.email == email
                        select em).FirstOrDefault();

                //did the email find a match?
                if (objI != null)
                {
                    String salt = objI.salt;

                    //salt and hash the plan text password.
                    String password = txtPasswordLogin.Text;
                    String pass_and_salt = password + salt;

                    // Create a new instance of the hash crypto service provider.
                    HashAlgorithm hashAlg = new SHA256CryptoServiceProvider();

                    // Convert the data to hash to an array of Bytes.
                    byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(pass_and_salt);

                    // Compute the Hash. This returns an array of Bytes.
                    byte[] bytHash = hashAlg.ComputeHash(bytValue);

                    // Optionally, represent the hash value as a base64-encoded string,
                    // For example, if you need to display the value or transmit it over a network.
                    string base64 = Convert.ToBase64String(bytHash);

                    //check if the password that was just salted and hashed matches the password in the database.
                    if (objI.password == base64)
                    {
                        //Checking if the password was the same, Showing a valid login.
                        //lblError.Text = "Valid Login";

                        //store the identity in the session object
                        Session["user_id"] = objI.user_id;
                        Session["user_name"] = objI.first_name = " " + objI.last_name;

                        //rediect to logged in homepage.
                        Response.Redirect("login_landing.aspx");
                    }
                    else
                    {
                        lblError.Text = "Invaild Login";
                    }
                }
                else
                {
                    lblError.Text = "Invalid Login";
                }
            }
        }