public static void GetComment() { SqlConnection conn = GetConnection(); string selStrng = "SELECT * FROM Comment"; SqlCommand selCmd = new SqlCommand(selStrng, conn); try { conn.Open(); SqlDataReader reader = selCmd.ExecuteReader(); while (reader.Read()) { if (reader["IsDeleted"].ToString() == "False") { Comment cmmnt = new Comment(new Guid(reader["PostId"].ToString())); cmmnt.Title = reader["Title"].ToString(); cmmnt.Id = new Guid(reader["ID"].ToString()); cmmnt.Body = reader["Body"].ToString(); cmmnt.CommentedDate = Convert.ToDateTime(reader["DateAdded"].ToString()); string isDel = reader["IsDeleted"].ToString(); if (isDel == "True") cmmnt.IsDeleted = true; else cmmnt.IsDeleted = false; Comment.Items.Add(cmmnt.Id, cmmnt); } } reader.Close(); } catch (SqlException ex) { throw ex; } finally { conn.Close(); } }
public ActionResult Add(CommentModel model) { int id; if (ModelState.IsValid) { try { User user = _sessionManager.User; Comment comment = null; if (user == null) { comment = new Comment() { Article = _articleService.GetArticle(model.ArticleId), AuthorMail = model.Email, Content = model.Content, Parent = model.ParentId > 0 ? _commentService.GetComment(model.ParentId) : null, AuthorIP = Request.UserHostAddress, Author = model.Author }; DateTime timeout = DateTime.Now.AddMonths(1); Response.Cookies.Add(new HttpCookie("Author", Server.UrlEncode(model.Author)) { Expires = timeout }); Response.Cookies.Add(new HttpCookie("Email", Server.UrlEncode(model.Email)) { Expires = timeout }); Response.Cookies.Add(new HttpCookie("Url", Server.UrlEncode(model.Url)) { Expires = timeout }); } else { comment = new Comment() { Article = _articleService.GetArticle(model.ArticleId), AuthorMail = user.Email, Content = model.Content, Parent = model.ParentId > 0 ? _commentService.GetComment(model.ParentId) : null, AuthorIP = Request.UserHostAddress, Author = user.UserName }; } id = _commentService.AddComment(comment); } catch (Domain.DomainException ex) { ViewBag.Error = ex.Errors.First().Value; return View("Error"); } } else { bool s = false; foreach (ModelState state in ModelState.Values) { foreach (ModelError error in state.Errors) { ViewBag.Error = error.ErrorMessage; s = true; break; } if (s) { break; } } return View("Error"); } return Redirect("/Article/" + model.ArticleId + "/" + _commentService.GetCommentPageIndex(id) + "#comment-" + id); }
public static void AddCommentToDB(Comment cmnt) { string insString = "INSERT INTO Comment (ID, Title, Body, DateAdded, PostId) VALUES (@id, @title, @body, @added, @postId)"; SqlConnection conn = GetConnection(); SqlCommand insCmd = new SqlCommand(insString, conn); insCmd.Parameters.AddWithValue("@id", cmnt.Id.ToString()); insCmd.Parameters.AddWithValue("@title", cmnt.Title); insCmd.Parameters.AddWithValue("@body", cmnt.Body); var date = cmnt.CommentedDate.ToString("yyyy-MM-dd HH:mm:ss"); insCmd.Parameters.AddWithValue("@added", date); insCmd.Parameters.AddWithValue("@postId", cmnt.getPost().Id); try { conn.Open(); insCmd.ExecuteNonQuery(); } catch (SqlException ex) { throw ex; } finally { conn.Close(); } PostCommentRelation PCR = new PostCommentRelation(cmnt); PstCmntRelationManager.SaveToDB(PCR); }