Hi,
I'm an absolute newbie to asp.net... and recently got a start by visiting th
e beginner developer learning center at the msdn website. I have gone throug
h 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 datab
ase 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 displa
y the sample data which I added. The problem is with the messages.aspx. I ha
ve 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 mi
ght 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 ima
ginary 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 star
ted.
use scratch
go
-- make the message table
drop table dbo.messages
go
create table dbo.messages(
MessageID bigint primary key,
ThreadID bigint not null,
MessageBody nvarchar(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,
ReplyToID)
values (1,1,'1',null)
insert into dbo. messages(messageID,threadID,messageBody,
ReplyToID)
values (2,1,'1-1',1)
insert into dbo. messages(messageID,threadID,messageBody,
ReplyToID)
values (3,1,'1-2',1)
insert into dbo. messages(messageID,threadID,messageBody,
ReplyToID)
values (4,1,'1-2-1',3)
insert into dbo. messages(messageID,threadID,messageBody,
ReplyToID)
values (5,1,'1-2-2',3)
insert into dbo. messages(messageID,threadID,messageBody,
ReplyToID)
values (6,1,'1-2-2-1',5)
insert into dbo. messages(messageID,threadID,messageBody,
ReplyToID)
values (7,1,'1-2-2-2',5)
insert into dbo. messages(messageID,threadID,messageBody,
ReplyToID)
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.ReplyToID,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.ReplyToID,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/
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment