Click here to Skip to main content
13,771,394 members
Click here to Skip to main content
Add your own
alternative version


102 bookmarked
Posted 17 May 2006

Web-based Day Rental Calendar

, 29 Sep 2006
Rate this:
Please Sign up or sign in to vote.
Online month-view calendar of items rented by the day, with user defined rental lists.

Day Rental


I was asked to make an online calendar for summer cottage rentals, with the following feaures:

  • month per page,
  • if one or more units is rented for a certain day, show all units in the list for that day,
  • ability to control the display order of rental units and add/subtract from the rental list,
  • color-coded entries lining up across the page,
  • MS Access database backend, and
  • password protected admin pages.


Hoping to find a shareware to meet my requirements, I found Paul Apostolos' excellent article, "Building an Event Calendar Web Application". This became my starting point.


In the tbl_place table, the place field holds the full rental unit name used in the dropdown listbox on the New and EditDel page. The abbre field is the rental header abbreviation (two letters) used for display. In the Datasheet view, put your own place and abbre values. dsplOrder is the display order of the cottages for each day. Change the numbering here to determine the order on the calendar. Leave blank (null) to omit that unit from being displayed.

Using the code

As Paul's article points out, each day on the calendar is rendered separately, so it's better to store whatever you need from the database once for the page you are displaying. The myCollection and MyDates are from Paul's article. I have added the last three arrays to keep track of the display requirements. These stores are filled and emptied for each day being displayed in Calendar1_DayRender().

//holds MyDate structs. 
//See function: void GetDBItems()
public ArrayList myCollection;

public struct MyDates
    public int _ID;
    public DateTime _date;
    public string _renter;
    public long  _place;
    public long _dsplOdr;

//abbreviations of rental items
public ArrayList rentalHeaders;

//displayed rental item that has a renter
public ArrayList placeDisplayOrder;

//ID of place is different than display order
public ArrayList placeID;


Calendar1_DayRender(): first, GetDBItems() fills the myCollection array with MyDates structs (database records) for the month we are displaying. ORDER BY and tbl_place.dsplOrder so a DataReader can make one pass and get each day with the correct cottage display order. Get the two digit rental headers. In the foreach loop, we check the date. Now, build an HTML line for that day.

private void Calendar1_DayRender(object sender, 
        System.Web.UI.WebControls.DayRenderEventArgs e)
    DateTime dayHold;
    bool dayTextHasChanged = false;
    temp = new StringBuilder();
    //display order
    placeDisplayOrder = new ArrayList();
    foreach(MyDates Item in myCollection)
            //start a new day
            //a list of cottages with renters for the day we are on
            //start by putting the span
            temp.Append("<span style='font-family:Arial; " + 

            //See Display() below
            Display(sequence, (int)Item._dsplOdr-1, Item._renter);
            dayTextHasChanged = true;
        }//end day compare
    }//end foreach

    if(dayTextHasChanged == true)
        //finally print any remaining 
        //rental headers after the last booking
        //See DisplayLast() below
        DisplayLast(sequence, (int)placeD

        e.Cell.Controls.Add(new LiteralControl(temp.ToString()));
    //reset sequence
}//End Calendar1_DayRender

Display(): adds to the HTML line we are building for the current day. The DataReader is going row by row through the database. When we start a new day, the value of sequence will be 0. pl is the cottage display number. Suppose that number is 3. The first for loop prints the rental headers 0, 1, and 2. When i hits 3, we print the renter. If it's not the first cottage with a renter for the current day, we go to the else statement. In the placeDisplayOrder array, if the last number minus the second to last number in the array isn't equal to 1, then we have to print the rentalHeaders in between those two cottages.

void Display(int sq, int pl, string renter)
    string textColor;
    //first time build string up to and including the first booking
        for(int i=sq; i<=pl; i++)
            textColor = GetTextColor(i);
            temp.Append("<font color='"+textColor+"'>");
        //if the index of the current place 
        //minus index of last place is not one
            //print rental headers between the two bookings
            for(int i=(int)placeDisplayOrder[sq-1]; i<=pl; i++)
                textColor = GetTextColor(i);
                temp.Append("<font color='"+textColor+"'>");
            //otherwise just print the next booking
            textColor = GetTextColor((int)placeDisplayOrder[sq-1]);
            temp.Append("<font color='"+textColor+"'>");

When the foreach loop in Calendar1_DayRender() hits a new date, we use DisplayLast() to check for any remaining rentalHeaders, and add them to the HTML string for that day. In other words, if there are 6 cottages for rent but the sixth cottage has no renter, we still want to display the rentalHeader for the last cottage.

//Display remaining headers, take the total number 
//of rental units minus the last unit with a booking you
//printed. Use the array of rentalHeaders to print 
//the headers at the end of the list you need.
void DisplayLast(int sq, int pl)
    int remainingHeaders = (int)Session["numRentals"]-pl;
    int i=0;
        string txtcolor = GetTextColor(pl+i);
        temp.Append("<font color='"+txtcolor+"'>");

In the calendar, day numbers are hyperlinks. When you click on one, the Calendar1_SelectionChanged() is called. First, grab all the bookings from that day into a DataReader. Build a hyperlink with a query string of the ID, cottage, and renter. Using the HtmlGenericControl class lets you run regular HTML tags from the server. The selectedday <span> tag is used to display the date. The daydetail_render <span> tag is used to display the hyperlink. The daydetail <div> tag encloses the other two tags, and is used to control when the other two are visible. Here's the HTML code inside the <form> tag:

<div id="daydetail" runat="server" Visible="False">
    <h3>Update or Delete: 
        <span id="selectedday" runat="server"></span>
    <span id="daydetail_render" runat="server"></span>

Here's the code-behind:

private void Calendar1_SelectionChanged(object sender, System.EventArgs e)
    string temp="";
    //reset record updated msg
    lblMsg.Text = "";
    string selDate = "#"+Calendar1.SelectedDate.ToShortDateString()+"#";
    string sql = "SELECT tbl_events.ID,," + 
                 " tbl_events.cust, " + 
                 "FROM tbl_events INNER JOIN tbl_place" + 
                 " ON tbl_events.place_ID = tbl_place.ID" + 
    "WHERE [date] = "+selDate+" ORDER BY tbl_place.ID";
    conn = new OleDbConnection(strConn);
        cmd = new OleDbCommand(sql, conn);
        dr = cmd.ExecuteReader();
        int i=0;
                temp +=  "<a href='EditDel.aspx?ID=" +dr["ID"]+"'>" + 
                         dr["place"]+": "+dr["cust"]+ "</a><br>";
            daydetail.Visible = true;
            daydetail_render.InnerHtml = temp;
            selectedday.InnerHtml = 
               Calendar1.SelectedDate.ToString("MMM d, yyyy");
            Calendar1.Attributes.Add("style", calendarPos(i));

All the bookings for a selected day appear above the calendar as hyperlinks. In Calendar1.Attributes.Add("style", calendarPos(i));, i is the number of lines of hyperlinks. calendarPos(i) changes the absolute position of the calendar down i times so you can see the hyperlinks.

Here's the display after choosing a day:

Clicking on a hyperlink (School House) tells the server to open the EditDel.aspx (same page you're on), only this time there's a query string attached. Bind() sees the query string: if(Request.QueryString["ID"]!= null) makes visible and populates another calendar control, a drop down box, and a text box with data from the record based on the ID. Calendar1.SelectionMode = (CalendarSelectionMode)0; turns off the day number hyperlinks on the main calendar to prevent the user from choosing another date before either updating or deleting the currently selected day or using the back button (which is the same as cancel). Here's the display after choosing a hyperlink.

When the Update button is clicked, IsAvailable() checks for double bookings and then displays a message.

bool IsAvailable(DateTime date, int place, int id)
    bool isAvail = true;
    foreach(MyDates Item in myCollection)
        if(id != Item._ID)
            if(Item._date == date && Item._place == place)
        lblMsg.Text = "<span style='font-family:Arial; " + 
                      "font-weight:bold;font-size:16px; color:red;'>" + 
                      "This date and cottage is " + 
                      "already booked.</span>";
        return false;
        lblMsg.Text = "<span style='font-family:Arial; " + 
                      "font-weight:bold;font-size:16px; color:green;'>" + 
        "Record updated.</span>";
        return true;


Here is the application in my inetpub/wwwroot/. All the public pages are in the cottrent root folder and don't require login. The admin folder holds the Add.aspx, EditDel.aspx, login.aspx, and the link page admin.aspx. The db folder holds the cottrent.mdb Access database file.


The Web.config file in the root folder keeps the connection strings, one for local and one for remote. The local version stays on my machine while the remote version is on the Windows host server. This way, I don't have to change any code or recompile between testing on the local server and uploading to the remote server. The remote string is commented out so this config file is used on my local machine. The code goes between <configuration> and <system.web>:

   <!--<span class="code-comment">Local Database connection string--></span>
   <add key="AccessConnStr" value="Provider=Microsoft.Jet.OLEDB.4.0;" + 

        "Data Source=C:\\inetpub\\wwwroot\

         \cottrent\\db\\cottrent.mdb;" />
   <!--<span class="code-comment">Remote Database connection string--></span>
   <!--<span class="code-comment"><add key="AccessConnStr" value="Provider=Microsoft.Jet.OLEDB.4.0;" + 
        Data Source=c:\\account\\domain\\wwwRoot\\
                    rent\\db\\cottrent.mdb;" />--></span>

In my C# code, I declare the string.

protected static string strConn = 

Also, in the root Web.config is the forms login. Set your username and password here.

<authentication mode="Forms" > 
    <forms loginUrl="admin/login.aspx" >
        <credentials passwordFormat="Clear">
          <user name="user" password="password"/>
</authentication > 

   <allow users="*" /> <!--<span class="code-comment"> Allow all users --></span>

Another Web.config in the admin folder allows the user named "user" and calls the login form. Everyone else is denied.

  <allow users="user" /> <!--<span class="code-comment"> Allow user --></span>
  <deny users="*" /> <!--<span class="code-comment"> deny everyone else --></span>

Printer friendly

Here's the HTML for dayrental.aspx generated by Visual Studio. The text in bold (see first page) is removed and put in the style section (see text in bold on the second snippet).

        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" 

    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <asp:Calendar id="Calendar1" 

                style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" 

                runat="server" Width="910px" 

                BorderColor="Black" SelectionMode="None" 


                <DayStyle Font-Size="12px" Font-Names="Verdana" 

                     Font-Bold="True" Wrap="False" HorizontalAlign="Left"

                     Height="100px" BorderWidth="1px" 

                     BorderStyle="Solid" Width="120px" 

                <NextPrevStyle Font-Size="14px"></NextPrevStyle>
                <DayHeaderStyle Font-Names="Verdana"></DayHeaderStyle>
                <SelectedDayStyle ForeColor="Black" 

                <TitleStyle Font-Size="18px" 

                     Font-Names="Verdana" Font-Bold="True"></TitleStyle>
                <OtherMonthDayStyle ForeColor="DarkGray">

To make the calendar print correctly, take the properties out of the <form> section and put them into the <style> section in <head>. Make one version for the screen, and one for print, using the CSS @media tag.

        <meta name="GENERATOR" 

              Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" 

        <style type="text/css"> 
            @media Screen { .dayrentcal { WIDTH: 910px; }
              .caldaystyle { FONT-SIZE: 12px; Height: 100px; }}
            @media Print { .dayrentcal { WIDTH: 460pt; }
              .caldaystyle { FONT-SIZE: 7.5pt; Height: 90px; }}
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <asp:Calendar id="Calendar1" 

                style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" 

                runat="server" BorderColor="Black" SelectionMode="None" 


                NextMonthText="Next&gt;&gt;" CssClass="dayrentcal">
              <DayStyle Font-Names="Verdana" Font-Bold="True" Wrap="False" 

                 HorizontalAlign="Left" BorderWidth="1px" BorderStyle="Solid" 

                 CssClass="caldaystyle" VerticalAlign="Top"></DayStyle>
              <NextPrevStyle Font-Size="14px"></NextPrevStyle>
              <DayHeaderStyle Font-Names="Verdana"></DayHeaderStyle>
              <SelectedDayStyle ForeColor="Black" BackColor="White">
              <TitleStyle Font-Size="18px" Font-Names="Verdana" Font-Bold="True">
              <OtherMonthDayStyle ForeColor="DarkGray"></OtherMonthDayStyle>

Where did the .dayrentcal in the <style> section come from? That is set in Visual Studio in the Properties window of the calendar. The property is called CssClass. Likewise, .caldaystyle is set in the same Properties window under DayStyle, and then CssClass. That's it. Now, when the user clicks the browser's Window menu or toolbox Print button, the calendar will fit on the page.

Installing locally

Unzip into inetpub/wwwroot. Create a virtual directory in IIS. Open with Visual Studio 2003 or above. Open Access and change the tbl_place table to suit your needs.

Installing remotely

Put the demo project download on your website.

  • Create a folder and set it to "Set as an application directory" and "Execute ASP/ASP.NET/PHP Scripts".
  • In application directory/Web.config - set the database connection string, username, and password.
  • In admin/Web.config - set the username the same as other Web.config.
  • Upload the file dayrental.aspx, denied.html, and Web.config into your application directory.
  • Create three directories inside your application directory: admin, bin, and db.
  • Put Add.aspx, admin.aspx, EditDel.aspx, login.aspx, and Web.config into the admin directory.
  • Put cottrent.dll into the bin directory.
  • Open Access and change the tbl_place table to suit your needs.
  • Put cottrent.mdb into the db directory.
  • Set the db directory to "Lock directory from web browsing (still allows access via website scripts)".
  • Set permissions to modify.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer (Senior)
United States United States
Senior Web Programmer for the Shubert Organization; I was also the Trombonist/Arranger for Black 47 for 25 years.

You may also be interested in...

Comments and Discussions

QuestionError dd/mm/yyyy Pin
DanielPino7428-Feb-15 22:42
memberDanielPino7428-Feb-15 22:42 
GeneralMy vote of 5 Pin
Global Analyser17-Mar-13 5:22
memberGlobal Analyser17-Mar-13 5:22 
Generalproject Pin
parul jaiswal17-Feb-09 2:12
memberparul jaiswal17-Feb-09 2:12 
GeneralThank you Pin
amgadhs23-Dec-08 1:26
memberamgadhs23-Dec-08 1:26 
GeneralError on the dowloaded demo. plss help Pin
dudegio5-Aug-07 0:27
memberdudegio5-Aug-07 0:27 
GeneralRe: Error on the dowloaded demo. plss help Pin
FredParcells5-Aug-07 6:13
memberFredParcells5-Aug-07 6:13 
GeneralRe: Error on the dowloaded demo. plss help Pin
dudegio5-Aug-07 16:28
memberdudegio5-Aug-07 16:28 
GeneralMulti function for calendar! Pin
Dung Hoang15-May-07 21:24
memberDung Hoang15-May-07 21:24 
GeneralRe: Multi function for calendar! Pin
FredParcells24-May-07 2:40
memberFredParcells24-May-07 2:40 
GeneralSource Pin
kro0314-May-07 13:00
memberkro0314-May-07 13:00 
GeneralRe: Source Pin
kro0315-May-07 4:55
memberkro0315-May-07 4:55 
GeneralRe: Source Pin
FredParcells24-May-07 2:43
memberFredParcells24-May-07 2:43 
QuestionHi Pin
isamir26-Feb-07 9:47
memberisamir26-Feb-07 9:47 
AnswerRe: Hi Pin
FredParcells26-Feb-07 14:19
memberFredParcells26-Feb-07 14:19 
GeneralNice work & my day view calendar control Pin
Dan Letecky24-May-06 9:31
memberDan Letecky24-May-06 9:31 
Nice work! I'm giving my 5.

Maybe you could find this useful: DayPilot - my open-source ASP.NET calendar control. You can try it online in a live demo.

My sites for smart .NET developers:
DayPilot - Open-source Outlook-like calendar control for ASP.NET
DotLucene - The fastest open source fulltext search engine for .NET
Seekafile Server - Flexible open-source search server
DotNetFirebird - Using Firebird SQL in .NET
GeneralRe: Nice work & my day view calendar control Pin
FredParcells24-May-06 13:47
memberFredParcells24-May-06 13:47 
GeneralnOOice Pin
aleebinmaden18-May-06 22:13
memberaleebinmaden18-May-06 22:13 
GeneralRe: nOOice Pin
FredParcells19-May-06 6:05
memberFredParcells19-May-06 6:05 

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 | Cookies | Terms of Use | Mobile
Web03 | 2.8.181119.1 | Last Updated 29 Sep 2006
Article Copyright 2006 by FredParcells
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid