Monday, February 13, 2012

ASP.net newbie trying to build a forum

Hi,
I'm an absolute newbie to asp.net... and recently got a start by visiting the beginner developer learning center at the msdn website. I have gone through all of the videos, and now I'm trying to build my first asp.net website... which would be an imaginary forum. I have the following tables in the database used in my application:
Categories
CategoryID int
CategoryName nvarchar(50)
CategoryDescription nvarchar(200)
Threads
ThreadID bigint(8)
CategoryID int
Subject nvarchar(50)
PostedDate datetime
PostedBy nvarchar(10)
Messages
MessageID bigint(8)
ThreadID bigint(8)
MessageBody nvarchar(200)
ReplyToID bigint(8)
User char(10)
You get the idea about what I'm trying to do.
Now, I have successfully written Categories.aspx, and Threads.aspx to display the sample data which I added. The problem is with the messages.aspx. I have no idea how to output data which would give me a means to sort and indent the messages according to the way the users would reply. I mean... users might reply to a message, and another might reply to the reply of that message... creating one big hierarchical mess. So I'm stuck... I can't build my imaginary forum.
Anyone experienced with writing code for forums with SQL server as the back end?
I am hoping to learn some new things about SQL server... maybe... XML? Which would solve my problem? I am already a little familiar with XML and XSL.
Many thanks in advance.
Mohamed Shafiee.
Hello Mohamed,
While this isn't an ASP.NET focused group, and while I don't believe SQLXML
is going to buy you anything here, here's an example that might get you started.
use scratch
go
-- make the message table
drop table dbo.messages
go
create table dbo.messages(
MessageID bigint primary key,
ThreadID bigint not null,
MessageBodynvarchar(200) not null,
ReplyToID bigint null foreign key references dbo.messages(messageID),
PostedBy varchar(50) not null default suser_sname())
go
set nocount on
go
-- some dummy messages
insert into dbo.messages(messageID,threadID,messageBody,ReplyT oID)
values (1,1,'1',null)
insert into dbo.messages(messageID,threadID,messageBody,ReplyT oID)
values (2,1,'1-1',1)
insert into dbo.messages(messageID,threadID,messageBody,ReplyT oID)
values (3,1,'1-2',1)
insert into dbo.messages(messageID,threadID,messageBody,ReplyT oID)
values (4,1,'1-2-1',3)
insert into dbo.messages(messageID,threadID,messageBody,ReplyT oID)
values (5,1,'1-2-2',3)
insert into dbo.messages(messageID,threadID,messageBody,ReplyT oID)
values (6,1,'1-2-2-1',5)
insert into dbo.messages(messageID,threadID,messageBody,ReplyT oID)
values (7,1,'1-2-2-2',5)
insert into dbo.messages(messageID,threadID,messageBody,ReplyT oID)
values (8,1,'1-2-1-1',4)
go
-- @.tid bigint is the ThreadID selected
-- @.rtm bigint is the Message user wants to reply to
drop procedure dbo.GetThreadForMessage
go
create procedure dbo.GetThreadForMessage(@.tid bigint, @.rtm bigint)
as begin
-- Get all the messages in this thread, "nested" by level
;with cte1 as (
select m1.messageID,m1.threadID,m1.MessageBody,m1.ReplyTo ID,m1.PostedBy,
cast(0 as int) as lvl
from dbo.messages m1
where m1.threadID = @.tid and m1.ReplyToID is null
union all
select mr.messageID,mr.threadID,mr.MessageBody,mr.ReplyTo ID,mr.PostedBy,
cte1.lvl + 1
from dbo.messages mr
join cte1 on mr.ReplyToID = cte1.MessageID
where mr.threadID = @.tid),
-- Get the reponses leading to the replying-to message.
cte2 as (
select replyToID from dbo.messages where messageID = @.rtm
union all
select m2.replyToID from dbo.messages m2
join cte2 on m2.messageID = cte2.replyToID
)
-- Last Message in thread
select * from cte1
where cte1.messageID = @.rtm
union all
-- Other messages in thread
select * from cte1
where cte1.messageID in (select ReplyToID from cte2)
union
-- Base message
select * from cte1 where cte1.replyToID is null
order by cte1.lvl desc
for xml path('message'),root('messages'),type
end
go
exec GetThreadForMessage 1,7
go
Thanks!
Kent Tegels
DevelopMentor
http://staff.develop.com/ktegels/

No comments:

Post a Comment