Calendar: Missing DataSet on VisibleMonthChanged Event

Posted on 2008-11-18
Last Modified: 2013-12-17

This code comes from the article: How to: Display Selected Dates from a Database in the Calendar Control found here:

The Problem:
Scrolling to another month (forward OR backward) causes an error:
Error Line 81 (in browser):    foreach (DataRow dr in dsHolidays.Tables[0].Rows)
Exception Details: System.IndexOutOfRangeException: Cannot find table 0.

It appears that it it not creating a DataSet, but I cannot find the bug.

Also, I'm pretty "green"to c#,  so point out any gotcha's you like.

Please Advise...

The database structure is a single table "Holidays" with the following (2) records:

id (Type: int, PK)   |   HolidayDate (Type: DateTime)   |  HolidayTItle (Type: varchar(50)
1                           |  11/27/2008 12:00:00 AM            |  Thanksgiving
2                           |  12/25/2008 12:00:00 AM            |  Christmas Day      
using System;

using System.Collections.Generic;

using System.Configuration;

using System.Data;

using System.Data.SqlClient;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;


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


    protected DataSet dsHolidays = new DataSet();


    protected void Page_Load(object sender, EventArgs e)


        if (!IsPostBack) 


            // display the current month

            Calendar1.VisibleDate = DateTime.Today;





    private void FillHolidayDataset()


        DateTime firstDate = new DateTime(Calendar1.VisibleDate.Year, Calendar1.VisibleDate.Month, 1);

        Label1.Text = "First Date: " + firstDate.ToShortDateString();


        DateTime lastDate = getFirstDayOfNextMonth();

        Label2.Text = "Last Date: " + lastDate.ToShortDateString();


        dsHolidays = GetCurrentMonthData(firstDate, lastDate);



    protected DateTime getFirstDayOfNextMonth() 


        // declare varibles to hold the current month and year number

        int monthNumber, yearNumber;


        // If @ end of year, move to 1st month of next year

        if (Calendar1.VisibleDate.Month == 12)


            monthNumber = 1;

            yearNumber = Calendar1.VisibleDate.Year + 1;




            // Get the next month in the year

            monthNumber = Calendar1.VisibleDate.Month + 1;

            yearNumber = Calendar1.VisibleDate.Year;


        DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);

        return lastDate;



    protected DataSet GetCurrentMonthData(DateTime firstDate, DateTime lastDate) 


    DataSet dsMonth = new DataSet();

    ConnectionStringSettings cs;

    cs = ConfigurationManager.ConnectionStrings["HolidaysConnectionString1"];

    String connString = cs.ConnectionString;

    SqlConnection dbConnection = new SqlConnection(connString);

    String query;

    query = "SELECT HolidayDate, HolidayTitle  FROM Holidays WHERE HolidayDate >= @firstDate AND HolidayDate < @lastDate";

    SqlCommand dbCommand = new SqlCommand(query, dbConnection);

    dbCommand.Parameters.Add(new SqlParameter("@firstDate", firstDate));

    dbCommand.Parameters.Add(new SqlParameter("@lastDate", lastDate));

    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(dbCommand);





    catch {}

    return dsMonth;


    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)


        DateTime nextDate;

        if (dsHolidays != null)


            foreach (DataRow dr in dsHolidays.Tables[0].Rows)


                nextDate = (DateTime)dr["HolidayDate"];

                if (nextDate == e.Day.Date)


                    e.Cell.Controls.Add(new LiteralControl("<br>" + dsHolidays.Tables[0].Rows[0].ItemArray[1].ToString()));

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







    protected void Calendar1_VisibleMonthChanged(object sender, MonthChangedEventArgs e)







Open in new window

Question by:the_sleeper
    LVL 8

    Accepted Solution

    On your GetCurrentMonthData function, make a breakpoint at sqlDataAdapter.Fill(dsMonth); and watch if it occures any exception, making your dataset returns null.

    Author Closing Comment

    Actually, I forgot to add a call to VisibleMonthChanged on the calendar in the .aspx page. When I added the call. Everything worked. But since you were "willing"..have some points :->


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now