Calendar: Missing DataSet on VisibleMonthChanged Event


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

Who is Participating?
tiagosalgadoConnect With a Mentor Commented:
On your GetCurrentMonthData function, make a breakpoint at sqlDataAdapter.Fill(dsMonth); and watch if it occures any exception, making your dataset returns null.
the_sleeperAuthor Commented:
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 :->

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.