?
Solved

Get Holidays from Database

Posted on 2014-09-23
4
Medium Priority
?
125 Views
Last Modified: 2014-09-29
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

0
Comment
Question by:RecipeDan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 2

Expert Comment

by:Vishal Patil
ID: 40340846
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

0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40341433
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.

Screenshot of calender
0
 
LVL 1

Accepted Solution

by:
RecipeDan earned 0 total points
ID: 40341667
I did some searching and found this link. It works good now. Thank you for your assistance

http://msdn.microsoft.com/en-us/library/vstudio/ms228044(v=vs.100).aspx
0
 
LVL 1

Author Closing Comment

by:RecipeDan
ID: 40349497
Found the solution
0

Featured Post

DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

764 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