sql server - sql command error in C# -


i have written code retrieve information 2 tables in database. when run error

column 'eaten_food.cardserial' invalid in select list because not contained in either aggregate function or group clause.

code:

 private void button8_click(object sender, eventargs e)  {         using (sqlconnection con = new sqlconnection(wf_abspres_food.properties.settings.default.dbconnectionstring))         {             con.open();             sqldataadapter = new sqldataadapter("select eaten_food.cardserial , eaten_food.date , eaten_food.turn , avb_food_count , reserve_count reserve inner join eaten_food on reserve.cardserial = eaten_food.cardserial group eaten_food.date", con);             sqlcommandbuilder comdbuilder = new sqlcommandbuilder(a);             datatable t = new datatable();             //t.locale = system.globalization.cultureinfo.invariantculture;             a.fill(t);             bindingsource3.datasource = t;              /// bind grid view binding source            reserve_datagridview.autoresizecolumns(datagridviewautosizecolumnsmode.allcellsexceptheader);             reserve_datagridview.readonly = true;             reserve_datagridview.datasource = bindingsource3;              reserve_datagridview.datasource = t;             con.close();         }     } 

how can solve it?

the problem sql query. if use group by cannot select columns not grouped or not aggregated(f.e. using min/max/avg/count).

so make working example in way, change old query here:

select eaten_food.cardserial,         eaten_food.date,         eaten_food.turn,         avb_food_count,         reserve_count    reserve         inner join eaten_food                 on reserve.cardserial = eaten_food.cardserial  group  eaten_food.date  

to:

select min(eaten_food.cardserial)as cardserial,         eaten_food.date,         min(eaten_food.turn) turn,         sum(avb_food_count) sumfoodcount,         sum(reserve_count) sumreservecount    reserve         inner join eaten_food                 on reserve.cardserial = eaten_food.cardserial  group  eaten_food.date  

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -