Solved

Get Holidays from Database

Posted on 2014-09-23
4
122 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 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