Beispiel #1
0
        // updatePost
        private void updatePost()
        {
            try {
                int  id   = int.Parse(this.dgvPost.CurrentRow.Cells[0].Value.ToString());
                post post = db.posts.SingleOrDefault(p => p.id == id);

                post.post_title   = tbTitle.Text;
                post.post_content = tbContent.Text;
                post.post_author  = int.Parse(cbbUser.SelectedValue.ToString());
                post.date_updated = DateTime.Now;
                var cbStatus = 0;
                if (cbPublish.Checked)
                {
                    cbStatus = cbStatus == 0 ? 1 : 0;
                }
                post.status = cbStatus;
                db.SaveChanges();
                MessageBox.Show("Update Sucessed");
                loadPost();
            }
            catch {
                MessageBox.Show("Update Failed");
            }
        }
Beispiel #2
0
        //add Post
        private void BtnAddPost_Click(object sender, EventArgs e)
        {
            var cbStatus = 0;

            if (cbPublish.Checked)
            {
                cbStatus = 1;
            }
            else
            {
                cbStatus = 0;
            }
            byte[] images = null;
            try {
                if (imgsrc.Length > 0)
                {
                    FileStream   fs = new FileStream(imgsrc, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fs);
                    images = br.ReadBytes((int)fs.Length);
                }
                var newPost = new post {
                    //post_author = int.Parse(cbbUser.SelectedValue.ToString()),
                    post_author  = LoginInfo.userId,
                    post_content = tbContent.Text,
                    post_title   = tbTitle.Text,
                    category     = int.Parse(cbbCategory.SelectedValue.ToString()),
                    status       = cbStatus,
                    date_created = DateTime.Now,
                    date_updated = DateTime.Now,
                    img          = images
                };


                db.posts.Add(newPost);
                db.SaveChanges();
                tbTitle.Clear();
                tbContent.Clear();
                cbPublish.Checked = false;
                picturePost.Image = Properties.Resources.ClickHere;
                if (role.Equals("USER") || role == "USER")
                {
                    loadPostByUser(id);
                }
                else
                {
                    loadPost();
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = $"{validationErrors.Entry.Entity.ToString()}:{validationError.ErrorMessage}";
                        raise = new InvalidOperationException(message, raise);
                    }
                }
                throw raise;
            }
        }