Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a database where there are 3 clubs dance club,drama club,science club. i have a query to count the number of clubs a student is a part of from the database.when i run the query in sql management studio it works perfectly fine.
When i add the query to c#.net making a connection and retrieving data using ExecuteReader(). Here the problem occurs.When i run the code and display the rows in gridview i do not get count but get as

id name count
1. xyz 1
2. xyz 1
3. pqr 1

i.e xyz is a part of 2 clubs and hence 2 rows

i want (ouput from sql management studio)

1. xyz 2
1. pqr 1
Posted
Comments
Praveen Kumar Upadhyay 23-Dec-14 3:31am    
What is the query that you are passing in the C# application.
Member 11316644 23-Dec-14 3:41am    
select b.* from(select id,name,'I' s_type,count(club)I_cnt from #temp_table where club='I'
group by id,name
union
select id,name,'II's_type,count(club)II_cnt from #temp_table where club='II'
group by id,name
union
select id,name,'III's_type,count(club)III_cnt from #temp_table where club='III'
group by id,name)b
order by id,s_type

1 solution

I think you need the Group By clause. Please check below link

GROUP BY Statement[^]


something like below.
SQL
SELECT ID, NAME , COUNT (STUDENTID) FROM CLUB   GROUP BY ID 
 
Share this answer
 
v3
Comments
Member 11316644 23-Dec-14 4:00am    
i have already used group by. And the query runs perfectly fine when executed in SQL management studio
Sibasisjena 23-Dec-14 5:01am    
Can you post your c# code

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