How to Find the Booked Dates Records in Calendar Control Highlighted with Different Colors

In this blog we will know how to find the booked dates records in the calendar control highlighted with different colors.

Scenario

Here the user is allowed to book the Marriage hall (Booked with Lawn/Booked without Lawn) according to his/her choice.After entering the data he/she can check it by using a calendar control according to his/her choice and then the respective day is booked with given condition with the provided conditions(Booked with Lawn/Booked without Lawn).

Booked with Lawn will be highlighted with green color.

Booked without Lawn will be highlighted with red color.

Insertbookdate.aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Insertbookdate.aspx.cs" Inherits="mandap_book.Insertbookdate" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:Label ID="Label1" runat="server" Text="Book date[mm/dd/yyyy]"

            Width="150px" BackColor="#FF9900" BorderColor="Yellow" ForeColor="Red"></asp:Label>

        <asp:TextBox ID="txt_date" runat="server"></asp:TextBox><br />

        <asp:Label ID="Label2" runat="server" Text="Choose Occasion Type" Width="150px"

            BackColor="#CC3300"></asp:Label>

        <asp:DropDownList ID="DropDown_occasion" runat="server">

        </asp:DropDownList>

    </div>

    <asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click"

        Text="Insert" />

    <asp:Label ID="lblmsg" runat="server" Text=""></asp:Label><br />

   

    <asp:Button ID="btn_show" runat="server" Text="Show" onclick="btn_show_Click" />

    </form>

</body>

</html>

Insertbookdate.aspx.cs code

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.SqlClient;

namespace mandap_book

{

    public partial class Insertbookdate : System.Web.UI.Page

    {

        string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlCommand com;      

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                DropDown_occasion.Items.Add("Choose Occasiontype");

                DropDown_occasion.Items.Add("Booked with Lawn");

                DropDown_occasion.Items.Add("Booked without Lawn");

            }

        }

        protected void btn_insert_Click(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection(connStr);

            com = new SqlCommand();

            com.Connection = con;

            com.CommandType = CommandType.Text;

            com.CommandText = "insert into test values(@ForDate,@Occasiontype)";

            com.Parameters.Clear();

            com.Parameters.AddWithValue("@ForDate", txt_date.Text);

            com.Parameters.AddWithValue("@Occasiontype", DropDown_occasion.SelectedValue);

            if (con.State == ConnectionState.Closed)

                con.Open();

            com.ExecuteNonQuery();

            con.Close();

            lblmsg.Text = "Data entered successfully!!!";

        }

        protected void btn_show_Click(object sender, EventArgs e)

        {

            Response.Redirect("Default.aspx");

        }

    }

}

Default.aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="mandap_book._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC"

            BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest"

            Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px"

            ShowGridLines="True" Width="220px" ondayrender="Calendar1_DayRender">

        <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />

        <SelectorStyle BackColor="#FFCC66" />

        <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />

        <OtherMonthDayStyle ForeColor="#CC9966" />

        <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />

        <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />

        <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt"

            ForeColor="#FFFFCC" />

        </asp:Calendar>

    </div>  

    <table style="width: 249px; height: 66px">

    <tbody><tr>

    <td style="width: 13px">

    &nbsp;<span id="Label4" style="color:#00C000;background-color:#00C000;border-color:#0000C0;border-width:1px;border-style:Solid;width:42px;">abcde</span></td>

    <td align="left">

    <span id="Label2" style="color:Black;text-align: left">Booked with Lawn</span></td>

    </tr>

    <tr>

    <td style="width: 13px">

    &nbsp;<span id="Label3" style="color:Red;background-color:Red;border-color:#0000C0;border-width:1px;border-style:Solid;width:42px;">abcde</span></td>

    <td align="left">

    <span id="Label6" style="color:Black;text-align: left">Booked without Lawn</span></td>

    </tr>

    <tr>

    <td style="width: 13px; height: 1px">

    &nbsp;<span id="Label8" style="color:#FFFFCC;background-color:#FFFFCC;border-color:#0000C0;border-width:1px;border-style:Solid;width:42px;">abcde</span></td>

    <td align="left">

    <span id="Label9" style="color:Black;text-align: left">Available</span></td>

    </tr>

    </tbody></table>

    <asp:Button ID="btn_back" runat="server" Text="Back" onclick="btn_back_Click" />

    </form>

</body>

</html>

Default.aspx.cs code

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.SqlClient;

namespace mandap_book

{

    public partial class _Default : System.Web.UI.Page

    {

        string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlCommand com;

        SqlDataAdapter sqlda;

        DataSet ds;

        string str;

        DataTable dt;

 

        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)

        {

            SqlConnection con = new SqlConnection(connStr);

            con.Open();

            str = "SELECT * FROM test";

            com = new SqlCommand(str,con);

            sqlda = new SqlDataAdapter(com);

            dt = new DataTable();

            sqlda.Fill(dt);

        

            DateTime occasionDate;

           

            string occasionType = string.Empty;

 

            if (dt.Rows.Count > 0)

            {

                for (int i = 0; i < dt.Rows.Count; i++)

                {

                  occasionDate = Convert.ToDateTime(dt.Rows[i]["ForDate"]);

                    occasionType = dt.Rows[i]["Occasiontype"].ToString();

                    if (e.Day.Date == occasionDate)

                    {

                        if (occasionType == "Booked with Lawn")

                        {

                            e.Cell.BackColor = System.Drawing.Color.Green;

                        }

                        else if (occasionType == "Booked without Lawn")

                        {

                            e.Cell.BackColor = System.Drawing.Color.Red;

                        }

                        else

                        {

                            e.Cell.BackColor = System.Drawing.Color.White;

                        }

                    }

                }

            }

        }

 

        protected void btn_back_Click(object sender, EventArgs e)

        {

            Response.Redirect("Insertbookdate.aspx");

        }

    }

}