Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
        //MySqlConnection objConn;
        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.
Posted
Comments
jmpapa 24-Feb-13 11:02am    
I've solved the problem by just removing the !IsPostBack condition. The problem now is, it just highlight dates in the current year. And also, how can I add a label per date that indicates the name of the holiday highlighted? Any help?
Naz_Firdouse 25-Feb-13 3:05am    
Please refer them...
hope it helps...
http://www.codepal.co.uk/show/Create_controls_dynamically_within_the_ASPNET_Calendar_control
http://forums.asp.net/p/1036174/1800067.aspx
jmpapa 25-Feb-13 5:16am    
The link is not working. It says, "403 - Forbidden: Access is denied. You do not have permission to view this directory or page using the credentials that you supplied."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900