Mark Adesina Omoniyi
Mark Adesina Omoniyi

Reputation: 449

Error in .net calendar control

I have the following code to show event on a calendar

Private scheduleData As New Dictionary(Of DateTime, String)(5)

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        scheduleData.Add(New DateTime(2011, 1, 9), "Vacation Day")
        scheduleData.Add(New DateTime(2011, 1, 18), "Budget planning meeting")
        scheduleData.Add(New DateTime(2011, 2, 5), "Conference call")
        scheduleData.Add(New DateTime(2011, 2, 10), "Meet with art director")
        scheduleData.Add(New DateTime(2011, 2, 15), "Vacation day")
        'scheduleData.Add(New DateTime(2011, 2, 15), "Go Shopping")
        Calendar1.Caption = "Personal Schedule"
        Calendar1.FirstDayOfWeek = System.Web.UI.WebControls.FirstDayOfWeek.Sunday
        Calendar1.NextPrevFormat = NextPrevFormat.ShortMonth
        Calendar1.TitleFormat = TitleFormat.MonthYear
        Calendar1.ShowGridLines = True
        Calendar1.DayStyle.HorizontalAlign = HorizontalAlign.Left
        Calendar1.DayStyle.VerticalAlign = VerticalAlign.Top
        Calendar1.DayStyle.Height = New Unit(75)
        Calendar1.DayStyle.Width = New Unit(100)
        Calendar1.OtherMonthDayStyle.BackColor = System.Drawing.Color.Cornsilk
        Calendar1.TodaysDate = New DateTime(2011, 1, 1)
        Calendar1.VisibleDate = Calendar1.TodaysDate
    End Sub

And the date render handler

Protected Sub Calendar1_DayRender(sender As Object, e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
        If scheduleData.ContainsKey(e.Day.[Date]) Then
            Dim lit As New Literal()
            lit.Text = "<br />"
            e.Cell.Controls.Add(lit)
            Dim lbl As New Label()
            lbl.Text = DirectCast(scheduleData(e.Day.[Date]), String)
            lbl.Font.Size = New FontUnit(FontSize.Small)
            e.Cell.Controls.Add(lbl)
        End If

    End Sub

However this work fine if i dont have more than one event in a day but if there are more than one event in a day i get the error below i.e if the line that reads 'scheduleData.Add(New DateTime(2011, 2, 15), "Go Shopping") is uncommented

An item with the same key has already been added.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: An item with the same key has already been added.

Kindly help me out.

Upvotes: 0

Views: 303

Answers (1)

barbiepylon
barbiepylon

Reputation: 911

With dictionaries you cannot have two entries with the same Key. Dictionaries are based on the [Key, Value] concept with unique keys.

Think of this like a Webster's dictionary. You would not define two different instances of the same word (keys) but instead you would have multiple definitions (values) under that word.

I would suggesting adding a time element or you may have to use a different data structure.

Upvotes: 1

Related Questions