Link to home
Start Free TrialLog in
Avatar of RecipeDan
RecipeDan

asked on

Get Holidays from Database

I am trying to get a list of holidays from a database to show in ASP:Calender. Nothing shows when I run the calendar. There is data in the database and I tested the connection and it works fine.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Calender
{
    public partial class Default : System.Web.UI.Page
    {
        private List<DateTime> HolidayDate = new List<DateTime>();
        private List<string> HolidayName = new List<string>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetHolidays();
            }
        }

        private void GetHolidays()
        {
            SqlConnection conn;
            SqlCommand comm;
            SqlDataReader reader;
            string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            conn = new SqlConnection(connectionString);
            comm = new SqlCommand("SELECT * FROM Holidays", conn);
            try
            {
                conn.Open();
                reader = comm.ExecuteReader();
                while (reader.Read())
                {
                    HolidayDate.Add((DateTime)reader[1]);
                    HolidayName.Add((string)reader[2]);
                }
                reader.Close();
            }
            finally
            {
                conn.Close();
            }    
        }

        protected void ApptCal_CalRender(object sender, System.Web.UI.WebControls.DayRenderEventArgs e)
        {
            string tooltipValue = string.Empty;
            if (IsEventDay(e.Day.Date, out tooltipValue))
            {
                e.Cell.BackColor = System.Drawing.Color.DarkGoldenrod;
                e.Cell.ToolTip = tooltipValue;
            }
            if (e.Day.IsWeekend)
            {
                e.Cell.BackColor = System.Drawing.Color.Black;
                e.Cell.ForeColor = System.Drawing.Color.White;
            }

            if (e.Day.IsOtherMonth)
            {
                e.Cell.Text = "";
            }
        }

        private bool IsEventDay(DateTime calenderDay, out string tooltipValue)
        {
            tooltipValue = string.Empty;
            for (int i = 0; i < HolidayDate.Count; i++)
            {
                if (HolidayDate[i] == calenderDay)
                {
                    tooltipValue = HolidayName[i];
                    return true;
                }
            }
            return false;
        }
        protected void ApptCal_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
        {
            GetHolidays();
        }
    }
}

Open in new window


        <asp:Calendar ID="ApptCal" Width="750px" FirstDayOfWeek="Sunday" OnVisibleMonthChanged="ApptCal_VisibleMonthChanged" DayNameFormat="Short" OnDayRender="ApptCal_CalRender" NextPrevFormat="ShortMonth" SelectedDayStyle-BackColor="SkyBlue" TodayDayStyle-BackColor="Yellow"  
        BorderWidth="2px" 
        Font-Names="Arial" 
        Font-Size="Medium" 
        ShowGridLines="true" 
        DayStyle-Height="48px" 
        DayStyle-Width="125px" 
        DayStyle-Wrap="true" 
        runat="server"/>

Open in new window

Avatar of Vishal Patil
Vishal Patil

Hello RecipeDan,

Replace your code of "IsEventDay" function with following code.

private bool IsEventDay(DateTime calenderDay, out string tooltipValue)
        {
            tooltipValue = string.Empty;
            for (int i = 0; i < HolidayDate.Count; i++)
            {
                if (HolidayDate[i].ToString("dd/MM/yyyy") == calenderDay.ToString("dd/MM/yyyy"))
                {
                    tooltipValue = HolidayName[i];
                    return true;
                }
            }
            return false;
        }

Open in new window

Avatar of RecipeDan

ASKER

That did not work. I took a screen shot so you can see. The calender loads fine but is not reading the data from the database. September 1st should be highlighted Goldenrod for Labor Day.

User generated image
ASKER CERTIFIED SOLUTION
Avatar of RecipeDan
RecipeDan

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
Found the solution