Click here to Skip to main content
15,905,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
--i want to highlight the holiday list in the calendar

--i am getting Object reference not set to an instance of an object.

--:aspx page..ondayrender is there

<asp:Calendar ID="clPopupCalendar" runat="server" Width="100%" CssClass="normalCalendar" Height="160px"
                       DayNameFormat="FirstLetter" ShowGridLines="True" OnDayRender="holidaylist" OnSelectionChanged="clPopupCalendar_SelectionChanged" >
                       <todaydaystyle forecolor="White" backcolor="Black"></todaydaystyle>
                       <SelectorStyle BackColor="Red"></SelectorStyle>
                       <nextprevstyle font-size="9pt" forecolor="#FFFFCC"></nextprevstyle>
                       <dayheaderstyle height="1px" cssclass="normalCalendarDayHeader"></dayheaderstyle>
                       <SelectedDayStyle CssClass="normalCalendarSelectedDay"></SelectedDayStyle>
                       <TitleStyle CssClass="normalCalendarTitle" BackColor="#6699CC"></TitleStyle>
                       <othermonthdaystyle forecolor="#FF0033"></othermonthdaystyle>

--code behind

 public partial class UserControls_PopUpCalendar : TMSWeb.TMSBasePage

    List<datetime> dtholidays = null;


 public List <datetime> GetPublicHolidays()
        List <datetime> list = new List<datetime>();
        list.Add(new DateTime(2013, 02, 07));
        return list;



protected void holidaylist(object sender, DayRenderEventArgs e)


        if (dtholidays.Contains(e.Day.Date))


            e.Cell.BackColor = System.Drawing.Color.Green;



---if (dtholidays.Contains(e.Day.Date))
--i get
--Object reference not set to an instance of an object.

--it seems everyting fine for me....but why is thie error ?
Object reference not set to an instance of an object.
Updated 30-Jun-13 23:07pm

dtholidays may be null, when you called GetPublicHolidays method
It must return list which should be assigned to dtholidays
dtholidays = GetPublicHolidays();

Set in PageLoad event of Page
Share this answer
Yes this exception is obvious, because dtholidays is null. You check for null before using the variable. Try this:
if(dtholidays != null){
    if (dtholidays.Contains(e.Day.Date))
        e.Cell.BackColor = System.Drawing.Color.Green;

As I can see your code, dtholidays is class level variable and it'll be reassigned on every postback and this will become null. Better, you store this list to session and whenever required fetch it from session.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900