protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Session["userID"] != null)
            {
                //connect
                using (DefaultConnection db = new DefaultConnection())
                {
                    if (!String.IsNullOrEmpty(Request.QueryString["id"]))
                    {
                        //get the id from the url
                        Int32 postID = Convert.ToInt32(Request.QueryString["id"]);

                        // look up post
                        post postObj = (from p in db.posts
                                        where p.id == postID
                                        select p).FirstOrDefault();

                        postObj.message = txtNewMsg.Text;

                        //save updated post
                        db.SaveChanges();

                        // redirect to user profile page
                        Response.Redirect("profile.aspx");
                    }
                }
            }
            else
            {
                Response.Redirect("default.aspx");
            }
        }
        protected void btnPost_Click(object sender, EventArgs e)
        {
            if (Session["userID"] != null)
            {
                // connect
                using (DefaultConnection db = new DefaultConnection())
                {
                    // create new post object in memory
                    post postObj = new post();
                    // get user id
                    Int32 userID = Convert.ToInt32(Session["userID"]);
                    String username = Session["username"].ToString();

                    // fill new post content
                    postObj.message = txtMessage.Text;
                    postObj.user_id = userID;
                    postObj.dislike_count = 0;
                    postObj.post_by = username;

                    // add post to database
                    db.posts.Add(postObj);

                    // save the new post
                    db.SaveChanges();

                    txtMessage.Text = "";
                    lblStatus.Text = "";

                    GetMessages();
                }
            } else
            {
                lblStatus.Text = "You must log in first to post";
            }
        }
        protected void grdUserMsg_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // identify post id
            Int32 postID = Convert.ToInt32(grdUserMsg.DataKeys[e.RowIndex].Values["id"]);

            // connect
            using (DefaultConnection db = new DefaultConnection())
            {
                post postObj = (from p in db.posts
                                where p.id == postID
                                select p).FirstOrDefault();

                // delete
                db.posts.Remove(postObj);
                db.SaveChanges();

                //refresh grid
                GetUserMsg();
            }
        }
        protected void btnSignup_Click(object sender, EventArgs e)
        {
            // connect
            using (DefaultConnection db = new DefaultConnection())
            {
                // create a new user
                user userObj = new user();

                // fill user name from sign up form input
                userObj.username = txtUsername.Text;

                // salt and hash the plain 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);

                userObj.password = base64;
                userObj.salt = salt;

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

                // redirect
                Response.Redirect("default.aspx");
            }
        }
        protected void grdMessages_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DislikeMsg")
            {
                Int32 index = Convert.ToInt32(e.CommandArgument);
                Int32 postID = Convert.ToInt32(grdMessages.DataKeys[index].Values["id"]);

                //connect
                using (DefaultConnection db = new DefaultConnection())
                {
                    post postObj = (from p in db.posts
                                    where p.id == postID
                                    select p).FirstOrDefault();
                    // increase dislike count
                    postObj.dislike_count++;

                    db.SaveChanges();

                    // refresh grid
                    GetMessages();
                }
            }
        }