private static bool LoadDiscussions(Forum forum, int startRow, int endRow) { bool res = false; try { forum.Clear(); string query = "MessagesList"; using (SqlConnection cnn = new SqlConnection(Configurations.ConnectionString)) { using (SqlCommand cmd = new SqlCommand(query, cnn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@ForumId", SqlDbType.SmallInt, 2, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Default, forum.ForumId)); cmd.Parameters.Add(new SqlParameter("@PageId", forum.PageId)); cmd.Parameters.Add(new SqlParameter("@StartRow", startRow)); cmd.Parameters.Add(new SqlParameter("@EndRow", endRow)); foreach (SqlParameter Parameter in cmd.Parameters) { if (Parameter.Value == null) { Parameter.Value = DBNull.Value; } } cnn.Open(); SqlDataReader reader = cmd.ExecuteReader(); if (reader != null && reader.HasRows) { // Every message belongs to a discussion in a forum Discussion discussion = null; DiscussionMessage message = null; DiscussionMessage parent = null; Int32 parentId = 0; Int32 discussionId = 0; Int32 messageId = 0; while (reader.Read()) { message = null; discussionId = Repository.Utils.Convert.ToInt32(reader["DiscussionId"]); parentId = Repository.Utils.Convert.ToInt32(reader["ParentId"]); messageId = Repository.Utils.Convert.ToInt32(reader["MessageId"]); // Update current discussion if (discussion != null) { // This message is in current discussion if (discussionId == discussion.Id) { // Do nothing. Current discussion is OK! } else { // This is a message inside a discussion // Or a root (a discussion) record so // Let's find discussion by it's id discussion = forum.FindDiscussion(discussionId); } } // If discussion not found, create and add it if (discussion == null) { discussion = new Discussion(discussionId, forum); forum.Add(discussion); if (messageId == discussionId) { // If this record is a discussion message message = discussion; } else { // This is a message inside a discussion that does not exist?! // An empty discussion created. Maybe filled later on // Let next step handle message related stuff } } // This is a message with an unknown parent inside a known discussion if (parentId <= 0 && message == null) { // Add it to the root of the discussion message = new DiscussionMessage(); discussion.Replies.Add(message); message.Parent = discussion; } else if (parentId > 0)// A new message { message = new DiscussionMessage(); // Find parent if (parent == null || parent.Id != parentId) { parent = discussion.FindMessage(parentId); } // Parent not found? Add it to the root of discussion if (parent == null) { parent = discussion; } parent.Replies.Add(message); message.Parent = parent; } message.Discussion = discussion; // At this point message must be at the right position // Try load it's data. // load message message.Id = messageId; message.IsVisible = Repository.Utils.Convert.ToBool(reader["IsVisible"]); message.IsAbuse = Repository.Utils.Convert.ToInt16(reader["IsAbuse"]); message.Type = (Repository.Memory.Forums.MessageTypes)Repository.Utils.Convert.ToInt16(reader["MessageType"]); message.InsertTime = Repository.Utils.Convert.ToDateTime(reader["InsertTime"]); message.UpdateTime = Repository.Utils.Convert.ToDateTime(reader["UpdateTime"]); message.UserId = Repository.Utils.Convert.ToInt64(reader["UserId"]); message.UserName = Repository.Utils.Convert.ToString(reader["UserName"]); message.UserIp = Repository.Utils.Convert.ToString(reader["UserIp"]); message.Subject = Repository.Utils.Convert.ToString(reader["MessageSubject"]); message.Body = Repository.Utils.Convert.ToString(reader["MessageBody"]); } reader.NextResult(); if (reader.Read()) { forum.TotalMessageCount = Repository.Utils.Convert.ToInt32(reader["TotalRows"]); } res = true; } cnn.Close(); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(string.Format("Exception:{0}", ex.ToString())); } return(res); }