Database Driven Event Calendar in ASP.Net 2.0

The sample in this article acts as an event driven calendar using an Asp:calendar control to display events that are database driven.

In normal life we use a Digital Diary to check our daily routine and events. I just thought of using an Asp:calendar control to display events that are database driven. In short the sample in this article acts as an event driven calendar.

 

In ASP.NET the Calendar control has the event called "OnDayRender", I have utilized this event in my article, that is very important to match the database date and current date on the calendar.

 

I have database called "Event" in which I have a "Cal_Event" table whose structure is as follows:

 

EventId int Identity(1,1) Unchecked,
Event_Title varchar(50) Unchecked,
Event_Date datetime Unchecked,
Event_Type int Unchecked

 

I am simply saving my events in the database with the Event date on which this event must occur.

 

Here is my code behind code that is easy to use and understand. I have used an ArrayList collection and Struct to store and compare dates and events.

 

Code behind code:


using System;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

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 System.Collections;

using System.Text;

 

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

{

    public static ArrayList MyColllection;

    //Structure

    public struct My_Date

    {

        public DateTime Cal_Date;

        public string Cal_Type;

        public string Cal_Title;

    }

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            MyColllection = Get_Event();

        }

    }

    public ArrayList Get_Event()

    {

        SqlConnection myCon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);

        SqlCommand myComd = new SqlCommand("SELECT * FROM Cal_Event",myCon);

        SqlDataReader myDataReader;

        try

        {

            myCon.Open();

            myDataReader = myComd.ExecuteReader();

            MyColllection = new ArrayList();

            My_Date temp;

            //Iterate through the data reader

            while(myDataReader.Read())

            {

                temp.Cal_Title = myDataReader.GetValue(1).ToString();

                temp.Cal_Date = Convert.ToDateTime(myDataReader.GetValue(2));

                temp.Cal_Type = myDataReader.GetValue(3).ToString();

                MyColllection.Add(temp);

            }

        }

        catch

        {}

        finally

        {

            myCon.Close();

        }

        return MyColllection;

    }

    public void Calendar1_DayRender(object o, DayRenderEventArgs e)

    {

        string FontColor;

        string compDate = "01/01/1900"; // Date to compare initially

        DateTime DayVal = Convert.ToDateTime(compDate);

        bool mItemDay = false;

        bool dayTextChanged = false;

        StringBuilder strTemp = new StringBuilder();

        foreach (My_Date temp_dt in MyColllection)

        {

            if ("01/01/1900" != temp_dt.Cal_Date.ToShortDateString())

            {

                if (dayTextChanged == true)

                {

                    break;

                }

                mItemDay = false;

                DayVal = temp_dt.Cal_Date;

            }

            else

            {

                mItemDay = true;

            }

            if (e.Day.Date == Convert.ToDateTime(temp_dt.Cal_Date.ToString("d")))

            {

                switch (temp_dt.Cal_Type)

                {

                    case "1" :

                        FontColor = "Blue";

                        break;

                    case "2":

                        FontColor = "Red";

                        break;

                    default:

                        FontColor = "Black";

                        break;

                }

                if (mItemDay == false)

                {

                    strTemp = new StringBuilder();

                }

                else

                {

                    strTemp.Append("<br>");

                }

                strTemp.Append("<span style='font-family:verdana;font-size:10px;font-weight:bold;color'");

                strTemp.Append(FontColor);

                strTemp.Append("'><br>");

                strTemp.Append(temp_dt.Cal_Title.ToString());

                strTemp.Append("</span>");

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

                dayTextChanged = true;

            }

        }

        if (dayTextChanged == true)

        {

            e.Cell.Controls.Add(new LiteralControl(strTemp.ToString()));

        }

    }

}

 

Advantages:


This article can be extended for:

 

1. Event management System.
2. Time management.
3. Project management.