Click here to Skip to main content
15,905,071 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, I have a question about taking data from database using multiple queries, and returning it to the asp.net c# code, and populate the resulting dataset to a single gridview.

I will show how the database tables look like.
My tables are :-

1) tbl_user [userid, username, email, phone]
2) tbl_skills [skillid, skillname]
3) tbl_qualification [qualification_id, qualification_name]
4) tbl_user_skill [userid, skill_id]
5) tbl_user_qualifiaction [userid, qualification_id]

I require that, based on userid, I need to create a comma separated list of skills, a comma separated list of qualifications, count of skills, count of qualifications, userid, username, email and phone.

For that I created the following stored procedure:-

SQL
ALTER PROCEDURE [dbo].[sp_loaduserdetailsingridview]
@flag int=0,
@userid int=0,
@skillnames varchar(max)=null,
@skillcount int=0, 
@qualnames varchar(max)=null, 
@qualcount int=0,
@qualid int=0,
@skillid int=0
as
if(@flag=0)
 begin
 select @skillnames=coalesce(@skillnames + ', ','') + skill_name from tbl_skill inner join 
 tbl_user_skill on tbl_skill.skill_id=tbl_user_skill.skill_id where tbl_user_skill.userid=@userid

select @qualnames=coalesce(@qualnames + ', ','') + qualification_name from tbl_qualification inner join
 tbl_user_qualification on tbl_qualification.qualification_id=tbl_user_qualification.qualification_id
 where tbl_user_qualification.userid=@userid

select @skillcount=count(skill_id) from tbl_user_skill where userid=@userid

select @qualcount=count(qualification_id) from tbl_user_qualification where userid=@userid

select @skillnames as skillnames, @skillcount as skillcount, @qualnames as qualnames, @qualcount as qualcount

select username, email, hone from tbl_user where userid=@userid
end


I require the last two select queries o be returned to the asp.net c# code, and populate them in a single gridview.
The gridview should look like the following format:-

C#
[username, skillnames, skillcount, qualnames, qualcount, email, phone]


I tried using a dataset and append the data using a single dataset, but failed to work. When I use multiple dataset, some fields in the gridview will be populated, others will be bank, and some fields are duplicated.

How to solve this.
Posted
Comments
indrajeet jadhav 5-Oct-13 0:17am    
U can use Joins
Gee Varghese 5-Oct-13 0:59am    
I tried "join", but I don't know how to link those tables, based on my needs.
indrajeet jadhav 5-Oct-13 1:18am    
As per my knowledge you have only option Using joins ..Bcoz using sub your subselect statements look more complex and its difficult to pretend outcome ..So you can go for the joins and subselect

hello friend can you please check it out this modified store procedure...


SQL
ALTER PROCEDURE [dbo].[sp_loaduserdetailsingridview]
	@flag INT=0,
	@userid INT=0,
	@skillnames VARCHAR(MAX)=NULL,
	@skillcount INT=0, 
	@qualnames VARCHAR(MAX)=NULL, 
	@qualcount INT=0,
	@qualid INT=0,
	@skillid INT=0
AS
IF(@flag=0)
BEGIN
	SELECT @skillnames=COALESCE(@skillnames + ', ','') + skill_name FROM tbl_skill 
		INNER JOIN  tbl_user_skill ON tbl_skill.skill_id=tbl_user_skill.skill_id 
	WHERE tbl_user_skill.userid=@userid
 
	SELECT @qualnames=COALESCE(@qualnames + ', ','') + qualification_name FROM tbl_qualification 
		INNER JOIN tbl_user_qualification ON tbl_qualification.qualification_id=tbl_user_qualification.qualification_id
	WHERE tbl_user_qualification.userid=@userid
 
	SELECT @skillcount=COUNT(skill_id) FROM tbl_user_skill 
	WHERE userid=@userid
 
	SELECT @qualcount=COUNT(qualification_id) FROM tbl_user_qualification 
	WHERE userid=@userid
	
	SELECT username
		, @skillnames AS skillnames
		, @skillcount AS skillcount
		, @qualnames AS qualnames
		, @qualcount AS qualcount
		, email
		, hone FROM tbl_user WHERE userid=@userid
		
END
 
Share this answer
 
Comments
Gee Varghese 5-Oct-13 6:04am    
@Tejas
This thing worked super fine.
I have a few more doubts with the same.
In my c# code, I am actually passing the userid using an iteration.
So, each time, this stored proc is called, a row is created.

1) How would I append each rows created in this way to a common gridview ?
2) I do not actually want to display the skill count and qualification count in the gridview, but instead I need to sort the above results based on skill count. How is that possible ?
hello profession

its true that you have hazy database that you need to marge as peek

but in IT there is always solution for Exception..


try good joining and Sub Queries
and for data format as you like [username, skillnames, skillcount, qualnames, qualcount, email, phone]

you need to use Coalesce() in build function of SQL

for more
Using Coalesce() in sqlserver[^]

http://msdn.microsoft.com/en-us/library/ms190349.aspx[^]

HAppy to help!!!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900