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.

Bobo BobowskiPosted Oct 23, 2012, 12:07 PM
can some one explain to me step by step how to start the calendar and how to configure. Thanx in advance
vini katyalPosted Sep 14, 2012, 4:06 AM
what are the fields in the table i am not able to open the database\
Abdul KaderPosted Aug 13, 2012, 12:43 AM
It works fine, but how to get multiple events?
Viresh RajputPosted Jul 23, 2012, 4:49 AM
very nice post....helpfull..thanks
Manjunath KoobihalPosted Jun 22, 2012, 5:44 AM
I want to display subject in calender control on each date..like time table in school..when we click on subject it has to show pop up message...help me to get this solution..
Manjunath KoobihalPosted Jun 22, 2012, 5:44 AM
I want to display subject in calender control on each date..like time table in school..when we click on subject it has to show pop up message...help me to get this solution..
mike farhanPosted Dec 30, 2011, 7:06 PM
My calendar comes empty as it is, the new info that i got from the table dont show!
sunil prakashPosted Nov 25, 2011, 1:09 AM
How to fetch all teh events from the two calendars and dispaly it as a webpart on the page ?
Suyog PatilPosted Dec 17, 2010, 1:15 AM
Your are using Sql object and not using sqlClient Namespace. Why you giving headache to developers. Better you don't give answer any more. Thank you
RairaiPosted May 5, 2010, 7:51 AM
When running from Visual Studio you are granted access to the database, but when running "Live" you must add a user in SQL Server Management studio, and then add user and pass to your connectionstring: Data Source=YOURSQLSERVERNAME\SQLEXPRESS;Initial Catalog=YourDatabaseName;Persist Security Info=True;User ID=YourUserName;Password=YourPassword;
Dorababu MekaPosted Oct 23, 2009, 12:28 AM
Dear MunirShaikh, Really Nice article. May i know how to do the same for every year. Like if i store for January 1st as New year it should be shown every year...
Penny BorelliPosted Sep 17, 2008, 3:17 AM
This calendar only allows for one event to be displayed per day. Is there a way around this?
TomeditedPosted Dec 29, 2007, 6:01 PMEdited Dec 29, 2007, 6:02 PM
I also get this error when I run it on web server. Code runs OK on localhost. Error page on server Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Line 71: foreach (My_Date temp_dt in MyColllection) Code than I think is causing the error is SqlConnection myCon = new SqlConnection (ConfigurationManager.AppSettings["ConnectionString"]); What would be my Connection String on Server? Thank you
jer carPosted Dec 27, 2007, 1:36 PM
Line 112 I get the above error when trying to run this code.