event calendar time display problem

maheother

New member
Joined
Jun 29, 2005
Messages
2
Programming Experience
1-3
Hi,

I am trying to set on my intranet website an online event calendar using ASP.NET vb.net and an access 2000 database.

Everything is working nicely since I can add new events,display and update them no problem.

The only problem is that when displayed on the calendar, all the

" event starting time" come in this format : 12/30/1899 7:30 Am

when I only want the time to display on the 7:30 AM like format.Please help !

You can find below my code :

HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[url="http://www.w3.org/TR/html4/loose.dtd"]http://www.w3.org/TR/html4/loose.dtd[/url]">
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">

Dim ds As New DataSet()

Protected Sub Page_Load(Src As [Object], E As EventArgs)
   'Note: The following two lines of code should be on the same line.
   Dim connectionstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("aspnet_calendar.mdb") + ";"
   Dim sql As String = "select * from events"
   
   'Create a DataAdapter
   Dim da As New OleDbDataAdapter(sql, connectionstring)
   
   'Fill the DataSet (ds)

   da.Fill(ds, "events")
End Sub 

Protected Sub eventscalendar_DayRender(Src As [Object], E As DayRenderEventArgs)
   'use a StringBuilder object for optimized string concatenation
   Dim strEvents As New StringBuilder()
   strEvents.Append("<span style=""font-size:80%"">")
   
   Dim row As DataRow
   For Each row In  ds.Tables("events").Rows
      Dim eventdate As DateTime = CType(row("eventdate"), DateTime)
      If eventdate.Equals(E.Day.Date) Then
         'strEvents.Append("<br />" + row["eventtext"]);
         ' MODIFICATICATION mhb CI-DESSOUS
   'CType(row("eventdate"), DateTime.Now.ToString("t")
         strEvents.Append(("<br />" + row("eventtext") + "<br />" + row("startingtime")))
      End If
   Next row 
   'Close off the string in the strEvents StringBuilder
   strEvents.Append("</span>")
   
   'Add the string to the cell in a LiteralControl
   E.Cell.Controls.Add(New LiteralControl(strEvents.ToString()))
End Sub 'eventscalendar_DayRender

</script>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<body>
<form runat="server">
<asp:calendar BackColor="#FFFFCC" DayHeaderStyle-BackColor="#FFFFFF" DayStyle-HorizontalAlign="right" Font-Size="16" Height="100%" ID="eventcalendar" NextPrevStyle-BackColor="#99FFCC" runat="server" ShowDayHeader="true" ShowGridLines="true" ShowNextPrevMonth="true" TitleFormat="MonthYear" TitleStyle-ForeColor="#0000FF" WeekendDayStyle-BackColor="#FFCCFF" Width="100%" 
  OnDayRender="eventscalendar_DayRender" align="center"></asp:calendar>
</form >
  
</div>
</body>
</html>

Please help, this is very urgent !
 
Last edited by a moderator:
Hello maheother and welcome to the forum.

I would change your append line to something like this:
VB.NET:
strEvents.Append("<br>" & row("eventtext") & "<br>" & _
  CType(row("startingtime"), DateTime).ToShortTimeString)
Notice that I concatenated the string using the ampersand (&) instead of a plus. This is 'better' practice.
I removed the forward slash from the <br> tags simply because I'm not that familiar with HTML code and don't know what they mean, but I do know that the <br> tag means a line break.
So what does the <br /> tag mean?
 
Thanks for your help Paszt but know I'm getting the following message error :
"CAST FROM TYPE 'DBNull' TO TYPE 'DATE' IS NOT VALID "

For your information I set in my access database "startingtime " to Date/Time Medium Time (5:14 PM for instance)

Any idea on what's happening ?


Maheother
 
It doesn't matter what the Access format is. That is only for display purposes within Access. The internal representation of a DateTime doesn't change and always includes a date and a time.

The error you are getting suggests that you have an "eventdate" field that is null. You can test for null values like this:
VB.NET:
If myRow("myField") Is DBNull.Value Then
	MessageBox.Show("Field is null.")
End If
or this
VB.NET:
If TypeOf myRow("myField") Is DBNull Then
 	MessageBox.Show("Field is null.")
 End If
 
Back
Top