I have a program that shows the weekly birthdays of members every week. This code is working perfectly, until the end of the month. Whenever we reach the end of the month and there are less than 7 days left in the current month, the code no longer lists the birthdays.
I know this is because the week is now spanning across two months, but I have tried everything and just can't figure out what I must do in order to fix this. This is probably a simple issue. Any help would be greatly appreciated. Here is my code:
public void CheckDayOfWeekAndReturnBirthdaysInDateRange()
{
//Check the current day of the week and make calculations according to find out which week to use.
if (DateTime.Now.DayOfWeek.ToString() == "Sunday")
{
DateTime weekStart = DateTime.Now.Date;
DateTime weekEnd = DateTime.Now.AddDays(7);
Birthdays = eventsService.ReturnBirthdaysForCurrentWeek(weekStart, weekEnd);
}
else if (DateTime.Now.DayOfWeek.ToString() == "Monday")
{
DateTime weekStart = DateTime.Now.AddDays(-1);
DateTime weekEnd = DateTime.Now.AddDays(6);
Birthdays = eventsService.ReturnBirthdaysForCurrentWeek(weekStart, weekEnd);
}
else if (DateTime.Now.DayOfWeek.ToString() == "Tuesday")
{
DateTime weekStart = DateTime.Now.AddDays(-2);
DateTime weekEnd = DateTime.Now.AddDays(5);
Birthdays = eventsService.ReturnBirthdaysForCurrentWeek(weekStart, weekEnd);
}
else if (DateTime.Now.DayOfWeek.ToString() == "Wednesday")
{
DateTime weekStart = DateTime.Now.AddDays(-3);
DateTime weekEnd = DateTime.Now.AddDays(4);
Birthdays = eventsService.ReturnBirthdaysForCurrentWeek(weekStart, weekEnd);
}
else if (DateTime.Now.DayOfWeek.ToString() == "Thursday")
{
DateTime weekStart = DateTime.Now.AddDays(-4);
DateTime weekEnd = DateTime.Now.AddDays(3);
Birthdays = eventsService.ReturnBirthdaysForCurrentWeek(weekStart, weekEnd);
}
else if (DateTime.Now.DayOfWeek.ToString() == "Friday")
{
DateTime weekStart = DateTime.Now.AddDays(-5);
DateTime weekEnd = DateTime.Now.AddDays(2);
Birthdays = eventsService.ReturnBirthdaysForCurrentWeek(weekStart, weekEnd);
}
else if (DateTime.Now.DayOfWeek.ToString() == "Saturday")
{
DateTime weekStart = DateTime.Now.AddDays(-6);
DateTime weekEnd = DateTime.Now.AddDays(1);
Birthdays = eventsService.ReturnBirthdaysForCurrentWeek(weekStart, weekEnd);
}
}
public void LoadDatagridComponents(DataGridView dataBirthdays)
{
dataBirthdays.AutoGenerateColumns = false;
dataBirthdays.Columns[0].Visible = true;
dataBirthdays.Columns[1].Visible = true;
dataBirthdays.Columns[2].Visible = true;
dataBirthdays.Columns[0].HeaderText = "Name";
dataBirthdays.Columns[1].HeaderText = "Surname";
dataBirthdays.Columns[2].HeaderText = "Birthday";
//Loop through each birthday member and change date of birth to month/day fromat.
int currentRow = 0;
foreach (DataGridViewRow row in dataBirthdays.Rows)
{
string birthday = Convert.ToDateTime(dataBirthdays.Rows[currentRow].Cells[2].Value).ToString("MMMM dd");
dataBirthdays.Rows[currentRow].Cells[2].Value = birthday;
currentRow++;
}
}
Then from there it goes here:
public List<Birthdays> ReturnBirthdaysForCurrentWeek(DateTime weekStart, DateTime weekEnd)
{
var birthdays = new List<Birthdays>();
int month = DateTime.Now.Month;
int dayStartOfWeek = weekStart.Day;
int dayEndOfWeek = weekEnd.Day;
using (MySqlConnection Conn = new MySqlConnection(Connect.sConnStr))
{
Conn.Open();
string sSql = "SELECT name, surname, date_birth FROM members WHERE MONTH(date_birth) = " + month + " AND DAY(date_birth) BETWEEN " + dayStartOfWeek + " AND " + dayEndOfWeek + ";";
MySqlDataReader reader = Connect.getDataCommand(sSql, Conn).ExecuteReader();
while (reader.Read())
{
var birthday = new Birthdays()
{
MemberName = reader["name"].ToString(),
MemberSurname = reader["surname"].ToString(),
MemberBirthday = reader["date_birth"].ToString()
};
birthdays.Add(birthday);
}
Conn.Close();
Conn.Dispose();
}
return birthdays;
}