Link to home
Start Free TrialLog in
Avatar of QPR
QPRFlag for New Zealand

asked on

Page Load not firing?

Firstly I should say that this isn't my code so bare with me.....
I d/l it from here if you need to see the original (zip file at foot of page)
http://aspnet.4guysfromrolla.com/articles/041603-1.2.aspx

I split the code and put the vb into the code behind file.
It has this sub...
Public Sub Page_Load()
        tempDate = Me.Calendar1.TodaysDate
        If Not (Page.IsPostBack) Then
            Get_DBItems()
        End If
    End Sub

and in Get_DBItems it errors on this line....
parameterDate.Value = tempDate.AddDays(-40) saying the result is not a date.
When I check the initial value of parameterDate.Value is "nothing"
Strange thing is that I put a breakpoint in the Page_Load sub and it never seems to get there, when I launch from VS2005 and goes straight to the breakpoint within Get_DBItems.

I'll post my aspx and aspx.vb below.. any ideas?
The aspx is inside a <asp:content>

<div id="daydetail" runat="server" visible="false">
<h2>Details for <span id="selectedday" runat="server" /></h2>

      <span id="daydetail_render" runat="server" />
</div>      

<div runat="server">
   <asp:Calendar id="Calendar1" OnDayRender="Calendar1_DayRender"
               OnSelectionChanged="Calendar1_SelectionChanged"
            OnVisibleMonthChanged="MonthChanged"
               DayStyle-Height="100" DayStyle-Width="75" DayStyle-HorizontalAlign="Left"
            DayStyle-verticalalign="Top"
            NextPrevFormat="FullMonth" SelectionMode="Day"
            TitleStyle-Font-Bold="False"
            TitleStyle-Font-Size="12" BackColor="white" BorderColor="#000000"
            CellPadding="2" CellSpacing="2" Runat="server"
            SelectedDayStyle-BackColor="#faebd7"
            SelectedDayStyle-ForeColor="#000000"
            OtherMonthDayStyle-ForeColor="#C0C0C0" DayStyle-BorderStyle="Solid"
            DayStyle-BorderWidth="1" TodayDayStyle-ForeColor="Black" Height="600"
            Width="750">
      </asp:Calendar><br><br>
      
</div>

then the code behind.....

Imports System.Data
Imports System.Data.SqlClient

Partial Class HR_Calendar_Default
    Inherits System.Web.UI.Page
    Public Structure MyDates
        Dim _Title As String
        Dim _Date As DateTime
    End Structure

    Public MyCollection As Collection
    Public tempDate As DateTime

    Public Sub Page_Load()
        tempDate = Me.Calendar1.TodaysDate
        If Not (Page.IsPostBack) Then
            Get_DBItems()
        End If
    End Sub

    Public Sub Get_DBItems()

        Dim myCommand As SqlCommand
        Dim ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("IntranetConnectionString").ConnectionString
        Dim myConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(ConnectionString)

        myCommand = New SqlCommand("GetHRCalendarItems", myConnection)
        myCommand.CommandType = CommandType.StoredProcedure

        '// Set to 40 days before the current value of tempDate
        Dim parameterDate As New SqlParameter("@_Date", SqlDbType.DateTime)
        parameterDate.Value = tempDate.AddDays(-40)
        myCommand.Parameters.Add(parameterDate)

        '// Set to 40 days after the current value of tempDate
        Dim parameterDate2 As New SqlParameter("@_Date_Plus_1", SqlDbType.DateTime)
        parameterDate2.Value = tempDate.AddDays(40)
        myCommand.Parameters.Add(parameterDate2)

        Dim myDataReader As SqlDataReader
        myConnection.Open()
        myDataReader = myCommand.ExecuteReader()

        Dim temp As MyDates
        MyCollection = New Collection()

        While (myDataReader.Read())
            '//Created each time through the loop to get a new item to load and add to the collection
            temp = New MyDates()

            temp._Title = myDataReader.Item("Title")
            temp._Date = CDate(myDataReader.Item("_Date"))

            MyCollection.Add(temp)


        End While

        '// Close
        myDataReader.Close()
        myConnection.Close()



    End Sub


    Public Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs)
        Dim Item As MyDates  '//Temporary storage for looping through the collection
        Dim DayHold As DateTime = "01/01/1900" '//Temporary date to check for multiple items per day
        Dim MultipleItemDay As Boolean = False

        '// Set to true if the date is in the collection and needs to be displayed
        Dim DayTextHasChanged As Boolean = False

        '//To build the string for the contents of the table cell
        '//I'll use the stingbuilder class instead of concatenation
        Dim temp As StringBuilder

        If IsNothing(MyCollection) = True Then
            Get_DBItems()
        End If

        For Each Item In MyCollection
            '//The collection is loaded in date order.  So, if the date is different than the DayHold variable
            '//And I've already found the right day in the collection, I can exit the for loop
            If DayHold <> Item._Date Then
                If DayTextHasChanged = True Then
                    Exit For
                End If
                MultipleItemDay = False
                DayHold = Item._Date
            Else
                MultipleItemDay = True
            End If

            '//If I've found a date matching the current date...I need to create the appropriate text
            If e.Day.Date = Item._Date.ToString("d") Then
                If MultipleItemDay = False Then
                    temp = New StringBuilder() '//Create a new stringbuilder object
                Else
                    temp.Append("<br>") '//add a seperator to the stringbuilder
                End If
                temp.Append(Item._Title.ToString())
                temp.Append("</span>")
                DayTextHasChanged = True '//Set the flag
            End If
        Next

        '//If there was an Item for this day, add the stringbuilder text to the table cell
        If DayTextHasChanged = True Then
            e.Cell.Controls.Add(New LiteralControl(temp.ToString()))
        End If

    End Sub

    Public Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim ConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("IntranetConnectionString").ConnectionString
        Dim myConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(ConnectionString)
        Dim myCommand As SqlCommand
        Dim myDataReader As SqlDataReader
        myCommand = New SqlCommand("GetHRCalendarSingleItem", myConnection)
        myCommand.CommandType = CommandType.StoredProcedure

        Dim parameter_Date As New SqlParameter("@_Date", SqlDbType.DateTime, 8)
        parameter_Date.Value = Calendar1.SelectedDate
        myCommand.Parameters.Add(parameter_Date)

        myConnection.Open()
        myDataReader = myCommand.ExecuteReader()

        Dim temp As String = ""
        Dim Category As String = ""

        While (myDataReader.Read())
            temp &= "Title : <a href=""edit.aspx?ID=" & myDataReader.Item("ID") & """>" & myDataReader.Item("Title") & "</a><br>Date : " & myDataReader.Item("_Date") & "<br><br>"
        End While
        daydetail.Visible = True
        daydetail_render.InnerHTML = temp
        myDataReader.Close()
        myConnection.Close()
        selectedday.InnerHTML = Calendar1.SelectedDate.ToString("d MMM, yyyy")
        tempDate = Calendar1.SelectedDate '//
        Get_DBItems()
    End Sub

    Public Sub MonthChanged(ByVal source As Object, ByVal e As MonthChangedEventArgs)
        '//Set the tempDatevariable to the value in the MonthChangedEventArgs NewDate property
        tempDate = e.NewDate

        '//Reload the collection
        Get_DBItems()
    End Sub

End Class
Avatar of gnoon
gnoon
Flag of Thailand image

>Public Sub Page_Load()
This is not valid signature of asp.net page load function.

It should be
Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Avatar of QPR

ASKER

True. Changed it problem remains.
When I launch it and it falls over I hover over  tempDate = Me.Calendar1.TodaysDate
Me.Calendar1.TodaysDate is "11/15/2007" which is correct (although in the wrong format)
tempDate is #12:00am#
The tempDate will never be Nothing because it's of type DateTime, which is NOT a class but structure.

The defaule value of tempDate will be DateTime.MinValue. If you try to decrease days backward from the min, the exception will be thrown say that it's out of range (between min and max dates).
Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
ASKER CERTIFIED SOLUTION
Avatar of skiltz
skiltz
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of QPR

ASKER

Thanks but parameterDate.value was nothing
Shouldn't tempDate be set to '11/15/2007' on Page load and then used further down in DB_GetItems as the SQlparameter?
Avatar of QPR

ASKER

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

Perfect! Many thanks.
One day I will need to try and get my head around all this public, private, protected stuff together with function signatures!
What's showing on the page if this code was run

Response.Write(tempDate.ToString("g"))
:) so fast