void AddUser() { try { string emailcheck = tbEmail.Text.ToString(); var check = db.users.Where(us => us.email.Equals(emailcheck)).SingleOrDefault(); if (check == null) { user user = new user { username = tbUsername.Text, password = tbPassword.Text, email = tbEmail.Text }; db.users.Add(user); db.SaveChanges(); MessageBox.Show("Add success", "Infomation", MessageBoxButtons.OK, MessageBoxIcon.Information); LoadData(); DataBinding(); } else { MessageBox.Show("User already!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception ex) { MessageBox.Show(ex.Message); throw; } }
//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, 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; loadPostByUser(); } 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; } }
private void BtnComment_Click(object sender, EventArgs e) { ctx.comments.Add(new comment() { content_text = tbComment.Text, time = DateTime.Now, post_id = _postSelect, author = LoginInfo.userId, }); ctx.SaveChanges(); tbComment.Clear(); PostDetail_Load(sender, e); }
private void BtnRegister_Click(object sender, EventArgs e) { String name = txtName.Text; String email = txtEmail.Text; String password = txtPassword.Text; var isCheck = cbTerm.Checked; if (String.IsNullOrEmpty(name) || String.IsNullOrEmpty(email) || String.IsNullOrEmpty(password) || !isCheck) { MessageBox.Show(@"Wrong Input"); } else { try { if (CheckEmailInDb()) { MessageBox.Show("Email exists !!!"); } else { if (checkPassword()) { var u = new user() { username = name, email = email, password = password }; db.users.Add(u); db.SaveChanges(); Mail m = new Mail(); m.sendMail(email.ToString()); setRoleUser(); MessageBox.Show("Register successed!!!"); LoginInfo.email = email; LoginInfo.role = "USER"; backMainForm(); } else { txtPassword.Text = ""; txtPassword2.Text = string.Empty; MessageBox.Show("Invalid password"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); //throw; } } }
private void btnAddPost_Click(object sender, EventArgs e) { var cbStatus = 0; if (cbPublish.Checked) { cbStatus = 1; } var newPost = new post { post_author = int.Parse((cbbUser.Text)), post_content = tbContent.Text, post_title = tbTitle.Text, status = cbStatus, date_created = DateTime.Now, date_updated = DateTime.Now }; using (var ctx = new AppEntities()) { if (ctx.users.Find(newPost.post_author) != null) { ctx.posts.Add(newPost); ctx.SaveChanges(); this.postsTableAdapter.Fill(this.appDataSet2.posts); } else { } } }
private void UpdatePassword(string inEmail, string inPassword) { (from p in ctx.users where p.email == inEmail select p).ToList().ForEach(x => x.password = inPassword); ctx.SaveChanges(); }
void Add() { try { role r = new role { role_name = tbRoleName.Text }; db.roles.Add(r); db.SaveChanges(); LoadData(); DataBinding(); } catch (Exception ex) { MessageBox.Show(ex.Message); throw; } }
void Add() { try { user_role us = new user_role { role_id = int.Parse(cbRole.SelectedValue.ToString()), user_id = int.Parse(cbUser.SelectedValue.ToString()) }; db.user_role.Add(us); db.SaveChanges(); MessageBox.Show("Add success!", "Infomation", MessageBoxButtons.OK, MessageBoxIcon.Information); LoadData(); DataBinding(); } catch (Exception ex) { MessageBox.Show(ex.Message); throw; } }
private void updateProfile() { try { var email1 = LoginInfo.email; user user = db.users.Where(u => u.email.Equals(email1)).FirstOrDefault(); user.username = txtName.Text; user.email = txtMail.Text; byte[] images = null; FileStream fs = new FileStream(imgsrc, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); images = br.ReadBytes((int)fs.Length); user.img = images; db.SaveChanges(); MessageBox.Show("Update successed") ; } catch (Exception ex) { MessageBox.Show("Update failed \n" + ex.Message); } }