I'm actually creating a method to get details for my asp Chart based on a certain datetime range. I've looked at these 2 links,
My method to retrieve data,
//Monthly Statistics
public int janLogin(String username)
{
int result = 0;
StringBuilder sqlCmd = new StringBuilder();
sqlCmd.AppendLine("SELECT COUNT(*) FROM AuditActivity WHERE Username = @getUsername AND DateTimeActivity BETWEEN @getFirstDT AND @getLastDT AND ActivityType = @getType");
try
{
SqlConnection myConn = new SqlConnection(DBConnectionStr);
myConn.Open();
SqlCommand cmd = new SqlCommand(sqlCmd.ToString(), myConn);
//DateTime
DateTime currentDT = DateTime.Today;
//Code Below gets only current Start and End of Current month
DateTime FirstDT = currentDT.AddDays(1 - currentDT.Day);
DateTime SecondDT = FirstDT.AddMonths(1).AddSeconds(-1);
cmd.Parameters.AddWithValue("@getUsername", username);
cmd.Parameters.AddWithValue("@getFirstDT", FirstDT);
cmd.Parameters.AddWithValue("@getLastDT", SecondDT);
cmd.Parameters.AddWithValue("@getType", "Login");
result = Convert.ToInt16(cmd.ExecuteScalar());
myConn.Close();
return result;
}
catch (SqlException ex)
{
logManager log = new logManager();
log.addLog("AuditNLoggingDAO.janLogin", sqlCmd.ToString(), ex);
return 0;
}
}
What i'm trying to actually do as you can see from the method name janLogin basically to get how many people logged in from 1 Jan 2017 00:00:00 to 31 JAN 2017. Current month is actually August. How can i actually get for all twelve months (Jan to Dec) the start and end month (from the bold example) for current year of 2017?
If i'm not wrong it's having to involve a for loop? Where the end count is 12. But i'm quite sure on how to do it.. There might be a duplicated question but i cant seem to find one that fits my requirement..
Appreciate any help. Thank you!