Solved

Page Load not firing?

Posted on 2007-11-14
9
1,123 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:QPR
  • 4
  • 3
  • 2
9 Comments
 
LVL 16

Expert Comment

by:gnoon
Comment Utility
>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)
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
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#
0
 
LVL 16

Expert Comment

by:gnoon
Comment Utility
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).
0
 
LVL 7

Expert Comment

by:skiltz
Comment Utility
Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 7

Accepted Solution

by:
skiltz earned 500 total points
Comment Utility
opps I mean

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

Author Comment

by:QPR
Comment Utility
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?
0
 
LVL 29

Author Comment

by:QPR
Comment Utility
"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!
0
 
LVL 16

Expert Comment

by:gnoon
Comment Utility
What's showing on the page if this code was run

Response.Write(tempDate.ToString("g"))
0
 
LVL 16

Expert Comment

by:gnoon
Comment Utility
:) so fast
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

18 Experts available now in Live!

Get 1:1 Help Now