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 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();

            }
        }