Click here to Skip to main content
15,886,518 members
Articles / Web Development / ASP.NET
Tip/Trick

Binding of Calendar Controls from Database

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
8 Mar 2014CPOL 25.2K   72   9   2
How to bind holiday list and information about those holidays from database

Introduction

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
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
      <tr>
        <td colspan="2" align="center">

        <asp:Label ID="lblName" runat="server" Text="Holiday 
Calender" Font-Size="Large" Font-Bold="true"></asp:Label>
        </td>
      </tr>
      <tr>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td>
            <asp:Calendar ID="Calendar1" runat="server" ondayrender="Calendar1_DayRender" 
                onvisiblemonthchanged="Calendar1_VisibleMonthChanged"></asp:Calendar>
        </td>
      </tr>
    </table>
    </div>
    </form>
</body>
</html> 

Write the code behind:

C#
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
    (ConfigurationManager.ConnectionStrings["connection"].ConnectionString.ToString());
    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)
        {
            GetHolidays();
        }
    }
    private void GetHolidays()
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from tblHoliday",con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                holidays.Add((DateTime)ds.Tables[0].Rows[i][1]);
                holidayname.Add((string)ds.Tables[0].Rows[i][2]);
            }
        }
        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)
    {
        GetHolidays();
    }}  

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.

License

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


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionEasy to Understand Pin
harendernegi198626-Sep-17 5:08
harendernegi198626-Sep-17 5:08 
QuestionDatabase Pin
Uttam Kumar Santra9-Dec-14 19:32
Uttam Kumar Santra9-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.