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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 :->

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.