Solved

Get Holidays from Database

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Crystal reports vb.net 2 39
2 questions 10 25
InputLanguage 1 25
How do you pull mopre then 1000 records using VB.NET Directory Services namespace 2 14
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

774 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