Click here to Skip to main content
13,453,348 members (46,827 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked
Posted 8 Mar 2014

Binding of Calendar Controls from Database

, 8 Mar 2014
Rate this:
Please Sign up or sign in to vote.
How to bind holiday list and information about those holidays from database


Many times, I had questions about how to make a holiday calendar. Holiday calendar means what are the public holidays and what are the optional holidays and user defined holidays. These holidays must be added to the calendar and should show that day with different colors. So here, I am posting a tip with all the codes, maybe this will help you.

Using the Code

The ASPX source code is as follows:

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <td colspan="2" align="center">

        <asp:Label ID="lblName" runat="server" Text="Holiday 
Calender" Font-Size="Large" Font-Bold="true"></asp:Label>
            <asp:Calendar ID="Calendar1" runat="server" ondayrender="Calendar1_DayRender" 


Write the code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page 
    SqlConnection con = new SqlConnection
    private List<DateTime> holidays=new List<DateTime>();
    private List<string> holidayname = new List<string>();

    protected void Page_Load(object sender, EventArgs e)
      // Calendar1.VisibleDate = DateTime.Now;
        if (!IsPostBack)
    private void GetHolidays()
            SqlCommand cmd = new SqlCommand("select * from tblHoliday",con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        catch { }
        finally { con.Close(); }
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        string tooltip = string.Empty;
        if (IsEventDay(e.Day.Date, out tooltip))
            e.Cell.BackColor = System.Drawing.Color.Pink;
            e.Day.IsSelectable = false;
            e.Cell.ToolTip = tooltip;
        if (e.Day.IsWeekend)
            e.Cell.BackColor = System.Drawing.Color.Black;
            e.Cell.ForeColor = System.Drawing.Color.White;
            e.Day.IsSelectable = false;            
    private bool IsEventDay(DateTime day, out string tooltipvalue)
        tooltipvalue = string.Empty;
        for (int i = 0; i < holidays.Count; i++)
            if (holidays[i] == day)
                tooltipvalue = holidayname[i];
                return true;
        return false;
    protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)

These are the entire code where you can highlight your holiday list and weekend list in a calendar from database.

Please note that a calendar control can't be bind data like gridview, it has a DayRender event. So you have to raise that event and write the code for that, and another event you have to raise that is VisbleMonthChanged so that whatever the holidays list is for the current month as well as next moth will show and will be bound with the calendar control. I have attached the code and database. Please download the attached files and run it.


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


About the Author

Software Developer
India India
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionEasy to Understand Pin
harendernegi198626-Sep-17 5:08
memberharendernegi198626-Sep-17 5:08 
QuestionDatabase Pin
Member 37367209-Dec-14 19:32
memberMember 37367209-Dec-14 19:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180318.3 | Last Updated 8 Mar 2014
Article Copyright 2014 by SanSkun
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid