c# - How to display data from a specific week? -
i want display data specific week giving date e.g. 2015-06-01
this table want use week
bookingid checkindate checkoutdate 1 2015-05-25 2015-05-31 2 2015-05-26 2015-06-03 3 2015-06-01 2015-06-07 4 2015-06-01 2015-06-12
here database manager code data week:
public static arraylist getstatsbyweek(int week, int year) { sqlconnection conn = new sqlconnection(); sqlcommand comm = new sqlcommand(); arraylist rmcount = new arraylist(); try { conn.connectionstring = gsm_conn_str; conn.open(); comm.connection = conn; comm.commandtext = "select count(bookingid) \"totalcount\" checkinout datepart(week,checkindate)=@week or datepart(week,checkoutdate)=@week , year(checkindate)=@year , year(checkoutdate)=@year"; comm.parameters.addwithvalue("@week", week); comm.parameters.addwithvalue("@year", year); sqldatareader dr = comm.executereader(); while (dr.read()) { roomcounts r = new roomcounts(); r.totalcount = (int)dr["totalcount"]; rmcount.add(r); } dr.close(); } catch (sqlexception e) { throw e; } { conn.close(); } return rmcount; }
this codes
datetime date = convert.todatetime(tbxstrtdate.text); double totalpercentage; double totalcount = 0; int flr2count = 0; int flr3count = 0; cultureinfo cul = cultureinfo.currentculture; int num = cul.calendar.getweekofyear(date, calendarweekrule.firstfourdayweek, dayofweek.sunday); //retrieve data arraylist rmc = hoteldb.getstatsbyweek(num, date.year);
error solved , there data problem.
e.g. if enter 2015-05-27
tbxstrtdate
, retrieve,no data shown though there data within week.
your query fine.
the problem difference of "week number calculation" in c# method , sql server datepart
function.
your function gives week = 21 datepart
gives week = 22.
so there no matching result query.
try following calculate @week
in code. worked test data.
int num = cul.calendar.getweekofyear(date, calendarweekrule.firstday, dayofweek.sunday);
if specific calendarweekrule.firstfourdayweek
in week calculation need find corresponding sql equivalent.
Comments
Post a Comment