Link to home
Start Free TrialLog in
Avatar of the_sleeper
the_sleeperFlag for United States of America

asked on

Calendar: Missing DataSet on VisibleMonthChanged Event

Greetings,

This code comes from the article: How to: Display Selected Dates from a Database in the Calendar Control found here: http://msdn.microsoft.com/en-us/library/ms228044.aspx

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;
            FillHolidayDataset();
        }
    }
 
    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;
        }
        else 
        {
            // 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);
    try
    {
        sqlDataAdapter.Fill(dsMonth);
    }
    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)
    {
 
        FillHolidayDataset();
    }
 
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of tiagosalgado
tiagosalgado
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of the_sleeper

ASKER

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 :->

sleeper