Hi! I'm doing a program that will highlight the dates in a calendar where the dates are from the database. I'm using the following code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using System.Drawing;
using System.Net;
namespace WebApplication1
{
public partial class cal : System.Web.UI.Page
{
protected DataSet dsHolidays;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Calendar1.VisibleDate = DateTime.Today;
FillHolidayDataset();
}
}
protected void FillHolidayDataset()
{
DateTime firstDate = new DateTime(Calendar1.VisibleDate.Year,
Calendar1.VisibleDate.Month, 1);
DateTime lastDate = GetFirstDayOfNextMonth();
dsHolidays = GetCurrentMonthData(firstDate, lastDate);
}
protected DateTime GetFirstDayOfNextMonth()
{
int monthNumber, yearNumber;
if (Calendar1.VisibleDate.Month == 12)
{
monthNumber = 1;
yearNumber = Calendar1.VisibleDate.Year + 1;
}
else
{
monthNumber = Calendar1.VisibleDate.Month + 1;
yearNumber = Calendar1.VisibleDate.Year;
}
DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
return lastDate;
}
protected DataSet GetCurrentMonthData(DateTime firstDate,
DateTime lastDate)
{
DataSet dsMonth = new DataSet();
string connectionString = "SERVER=localhost;DATABASE=firstschema;UID=root;PASSWORD=1234;";
MySqlConnection dbConnection = new MySqlConnection(connectionString);
String query;
query = "SELECT Task_Date FROM nametable WHERE Task_Date >= @firstDate AND Task_Date < @lastDate";
MySqlCommand dbCommand = new MySqlCommand(query, dbConnection);
dbCommand.Parameters.Add(new MySqlParameter("@firstDate", firstDate));
dbCommand.Parameters.Add(new MySqlParameter("@lastDate", lastDate));
MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter(dbCommand);
try
{
sqlDataAdapter.Fill(dsMonth);
}
catch {}
return dsMonth;
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
DateTime nextDate;
if (dsHolidays != null)
{
foreach (DataRow dr in dsHolidays.Tables[0].Rows)
{
nextDate = (DateTime)dr["Task_Date"];
if (nextDate == e.Day.Date)
{
e.Cell.BackColor = System.Drawing.Color.Pink;
}
}
}
}
protected void Calendar1_VisibleMonthChanged(object sender,MonthChangedEventArgs e)
{
FillHolidayDataset();
}
}
}
My problem is, it only highlight the dates present in the current month. When I move the calendar into the next month, no highlights is shown. Also, when I return to the previous month, the highlighted dates before are gone.
How can I fixed this? Thank you in advance.