public static List<Comment> FindCommentsByItemID(string itemID) { List<Comment> result = new List<Comment>(); string connectionString = WebConfigurationManager.ConnectionStrings["defaultConnectionString"].ToString(); SqlConnection sqlConn = new SqlConnection(connectionString); sqlConn.Open(); string cmdString = "SELECT * FROM Comment WHERE ParentCommentID = 0 AND ItemID = @itemID ORDER BY ModifiedDate DESC"; SqlCommand sqlCmd = new SqlCommand(cmdString, sqlConn); sqlCmd.Parameters.Add(new SqlParameter("itemID", itemID)); SqlDataReader sqlDataReader = sqlCmd.ExecuteReader(); if (sqlDataReader.HasRows) { while (sqlDataReader.Read()) { Comment newComment = new Comment(); FillComment(sqlDataReader, newComment); newComment.childComments = GetChildComments(newComment.CommentID); result.Add(newComment); } sqlDataReader.Close(); } return result; }
static void FillComment(SqlDataReader sqlDataReader, Comment newComment) { newComment.CommentID = (int)sqlDataReader["CommentID"]; newComment.ParentCommentID = (int)sqlDataReader["ParentCommentID"]; newComment.ItemID = (string)sqlDataReader["ItemID"]; newComment.UserID = (int)sqlDataReader["UserID"]; newComment.Text = (string)sqlDataReader["Text"]; newComment.ModifiedDate = (DateTime)sqlDataReader["ModifiedDate"]; }
public static void AddComment(Comment newComment) { string connectionString = WebConfigurationManager.ConnectionStrings["defaultConnectionString"].ToString(); SqlConnection sqlConn = new SqlConnection(connectionString); sqlConn.Open(); string cmdString = "INSERT INTO [Comment] (ParentCommentID, ItemID, UserID, Text, ModifiedDate) VALUES (@parentCommentID, @itemID, @userID, @text, @modifiedDate)"; SqlCommand sqlCmd = new SqlCommand(cmdString, sqlConn); sqlCmd.Parameters.Add(new SqlParameter("parentCommentID", newComment.ParentCommentID)); sqlCmd.Parameters.Add(new SqlParameter("itemID", newComment.ItemID)); sqlCmd.Parameters.Add(new SqlParameter("userID", newComment.UserID)); sqlCmd.Parameters.Add(new SqlParameter("text", newComment.Text)); sqlCmd.Parameters.Add(new SqlParameter("modifiedDate", newComment.ModifiedDate)); sqlCmd.ExecuteNonQuery(); sqlConn.Close(); }
public static void AddComment(Comment newItem) { newItem.Text = HttpUtility.HtmlEncode(newItem.Text); CommentRepository.AddComment(newItem); }
protected void ButtonCommitComment_Click(object sender, EventArgs e) { if (AuthenticationService.GetUsername() == null) { Response.Redirect(@"~\Account\Login.aspx"); return; } Comment newComment = new Comment(); newComment.ParentCommentID = 0; newComment.UserID = AuthenticationService.GetUser().UserID; newComment.ItemID = itemID; newComment.ModifiedDate = DateTime.Now; newComment.Text = TextBoxComment.Text; CommentService.AddComment(newComment); Response.Redirect(Request.Url.ToString()); }