Solved

Get Holidays from Database

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now