Click here to Skip to main content
15,909,747 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I having a problem while using Left Join to bind data to gridview, maybe i got miss out somethig. below is my coding and error msg, please help me take a look, thanks

two table detail
Table Memberlist
Member_no  |  Member_id  |  Member_status
1      |      lee      |    normal
2      |      admin    |    admin
3      |      tan      |    normal

Table GainPointList
gpmember_id   |   gp_date_start        |   gp_date_end
lee       |       4/20/2013 9:18:17 PM  |   4/20/201 9:18:17 PM
lee       |       4/20/2013 9:18:17 PM  |  4/20/201 9:18:17 PM
lee       |       4/20/2013 9:18:17 PM  |  4/20/201 9:18:17 PM
lee       |       4/20/2013 9:18:17 PM  |  4/20/201 9:18:17 PM
tan       |       4/20/2013 9:18:17 PM  |  4/20/201 9:18:17 PM
tan       |       4/20/2013 9:18:17 PM  |  4/20/201 9:18:17 PM


coding part
C#
SqlCommand Cmd = new SqlCommand("Select (ML.member_no) as vmemberno, (ML.member_id) as vmemberid, COUNT(DISTINCT GPL.gpmember_id) as psmark, Min(GPL.gp_date_start) as psdatestart, Min(GPL.gp_date_end) as psdateend, (ML.member_status) as vmemberstatus From (memberlist ML LEFT JOIN gainpointlist GPL ON ML.member_id = GPL.gpmember_id) GROUP BY member_id", condatabase);

DataTable Table = new DataTable();

SqlDataAdapter Adapter = new SqlDataAdapter(Cmd);

Adapter.Fill(Table);

gvmemberinfo.DataSource = Table;

gvmemberinfo.DataBind();


ERROR MSG
Column 'memberlist.member_no' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. <br />
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. <br />
<br />
Exception Details: System.Data.SqlClient.SqlException: Column 'memberlist.member_no' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.


Source Error: 
Line 89:         	    DataTable Table = new DataTable();
Line 90: 	            SqlDataAdapter Adapter = new SqlDataAdapter(Cmd);
Line 91: 	            Adapter.Fill(Table); // error msg
Line 92:
Posted
Updated 16-May-13 9:43am
v3
Comments
Member 9581488 16-May-13 14:11pm    
Group by member_id,Member_no
try above and see if it works

The message is telling you exactly what is wrong: you have not referenced the columns correctly in your sql. Add the correct columns to the group by clause. Hint: any column that is not part of an aggregate function should be in the group by clause.
 
Share this answer
 
Comments
Maciej Los 16-May-13 15:50pm    
+5!
Mark Merrens told you excatly what to do, but i would suggest you to read these:
Aggregate Functions[^]
Use Aggregate Functions[^]
Aggregate Functions (Transact-SQL)[^]
Using Aggregate Functions in the Select List[^]
 
Share this answer
 

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