I'm going to go back to my schema (related question) - purely because it's quicker for me than trying to reproduce yours with data, etc. but hopefully this will help (You'll just have to map it back onto your structures). This answer may get heavily down voted - as it does not answer the question posed - but suggests a way to avoid the complex binding that would be required (having now seen how the output of the stored procedure is going to be used).
If your schema was like this:-
create table tbl_Inquiry_master (
id int,
body varchar(1024)
);
create table tbl_Inquiry_history (
id int,
inquiry_id int,
body varchar(1024)
);
create table tbl_question (
id int,
inquiry_id int,
body varchar(1024)
);
create table tbl_answer (
id int,
question_id int,
body varchar(1024)
);
insert into tbl_Inquiry_master values (1, 'inquiry one');
insert into tbl_Inquiry_master values (2, 'inquiry two');
insert into tbl_Inquiry_master values (3, 'inquiry three');
insert into tbl_Inquiry_master values (4, 'inquiry four');
insert into tbl_Inquiry_history values (1, 1,'history 1 (relates to inquiry 1)');
insert into tbl_Inquiry_history values (2, 3,'history 2 (relates to inquiry 3)');
insert into tbl_question values (1,1,'inquiry one question one')
insert into tbl_question values (2,1,'inquiry one question two')
insert into tbl_question values (3,2,'inquiry two question one')
insert into tbl_question values (4,4,'inquiry four question one')
insert into tbl_answer values (1,1,'answer 1 to question 1')
insert into tbl_answer values (2,1,'answer 2 to question 1')
insert into tbl_answer values (3,2,'answer 1 to question 2')
insert into tbl_answer values (4,4,'answer 1 to question 4')
insert into tbl_answer values (5,4,'answer 2 to question 4')
insert into tbl_answer values (6,4,'answer 3 to question 4')
You could have a stored procedure like this:-
create procedure [dbo].[ViewMessageThreads] (
@inquiry_id int)
AS
BEGIN
create table #return (
FromTable varchar(10),
sort1 int,
sort2 int,
sort3 int,
body varchar(1024),
cargo1 datetime,
cargo2 varchar(50)
)
insert into #return
select 'master', 1, 0, 0, m.body , null, null
from tbl_Inquiry_master m
where m.id=@inquiry_id
insert into #return
select 'history', 2, 0, 0, h.body , null, null
from tbl_Inquiry_history h
where h.inquiry_id=@inquiry_id
insert into #return
select 'question', 3, q.id, 0, q.body , GETDATE(), 'user'
from tbl_question q
where q.inquiry_id=@inquiry_id
insert into #return
select 'answer', 3, q.id, a.id, a.body , GETDATE(), 'staff'
from tbl_answer a
join tbl_question q on q.id=a.question_id
where q.inquiry_id=@inquiry_id
/*
-- uncomment this block to update the type in the way you appear
-- to want it set, I'm leaving it commented in my answer as I
-- cannot confirm the logic works - as I don't have your schema/data
-- and the code is, therefore, untested.
-- this logic could be moved to the top of the SP and the variable could be
-- substituted into each SELECT, so that the UPDATE would then not be required
declare @Type_name = @varchar(50) -- I don't know the dimensions of this type
select top 1 @Type_name=t.Type_name
from tbl_Type t
join tbl_Login_master l on l.Id=t.Id
join tbl_User_master u on u.Id=l.Id
join tbl_Inquiry_History h on h.User_id=u.Id
where (h.Inquiry_id=@inquiry_id))
update #return set cargo2=@Type_name
*/
select *
from #return
order by sort1, sort2, sort3
end
and calling it like:-
exec [dbo].[ViewMessageThreads] 1
exec [dbo].[ViewMessageThreads] 2
exec [dbo].[ViewMessageThreads] 3
exec [dbo].[ViewMessageThreads] 4
which would produce:-
which would produce output that should be easier for you to bind onto, rather than that output by the SP in your question.
You can use the values in the fromtable and sort columns to make decisions in your repeater about how to format the other columns.
You'll have to play around with populating the cargo slots (you may need more) and the sort order - as I'm not sure where/whether you want any output from the History/Master tables. Hopefully this will head you in the right direction.