Print Page | Close Window

Using the DateTime Control.

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=11121
Printed Date: 29 March 2026 at 9:20am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Using the DateTime Control.
Posted By: davidshq
Subject: Using the DateTime Control.
Date Posted: 07 July 2004 at 1:23pm

:-) Keeping the ASP.NET boards hopping. As mentioned earlier, I'm working on a Today in History Script (I actually wrote one that works in ASP...and figured it'd probably be best to start where I left off)...Anyways, my problem this time is in using DateTime. I guess I'm doing something wrong? Any suggestions on where my code is wrong would be appreciated:
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data
Imports System.Data.OleDb


Public Class TodayEvents
  Inherits Page
  Protected lblTodayEvents as Label
   Private Sub Page_Load(sender as Object, e as EventArgs) Handles MyBase.Load
     Dim DBConnection as New OleDBConnection()
     DBConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=civilwar.mdb"
     DBConnection.Open
     Dim DBReader as OleDBDataReader
     Dim TodaysDay as DateTime = DateTime.Now
     TodaysDay = TodaysDay.Day.ToString
     Dim TodaysMonth as DateTime= DateTime.Now
     TodaysMonth = TodaysMonth.Month.ToString
     Dim DBReadData as New OleDBCommand()
     DBReadData.Connection = DBConnection
     DBReadData.CommandText = "Select * from tblToday WHERE TodaysDay=mDay and TodaysMonth=mMonth"
     DBReader = DBReadData.ExecuteReader()
     DO WHILE DBReader.Read()
       lblTodayEvents.Text = lblTodayEvents.Text
    lblTodayEvents.Text &= "<tr><td>" & DBReader("mMonth") & "/"
    lblTodayEvents.Text &= DBReader("mDay") & "/"
    lblTodayEvents.Text &= DBReader("mYear") & " - "
    lblTodayEvents.Text &= DBReader("mEvent") & "<br>"
  Loop
  DBReader.Close()
  DBConnection.Close()
   End Sub
End Class

Respectfully,
David.



-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.



Replies:
Posted By: davidshq
Date Posted: 07 July 2004 at 1:28pm

Just to highlight the code in question it is:
' Find today's date so it can be compared against data.
     Dim TodaysDay as DateTime = DateTime.Now
     TodaysDay = TodaysDay.Day.ToString
     Dim TodaysMonth as DateTime= DateTime.Now
     TodaysMonth = TodaysMonth.Month.ToString
Error is:
Cast from string "7" to type 'Date' is not valid.
Fun. :-P



-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.


Posted By: Mart
Date Posted: 07 July 2004 at 1:31pm

Instead of

Dim TodaysDay as DateTime = DateTime.Now
     TodaysDay = TodaysDay.Day.ToString
     Dim TodaysMonth as DateTime= DateTime.Now
     TodaysMonth = TodaysMonth.Month.ToString

Use

Dim TodaysDay As String = Now.Day.ToString

Dim TodaysMonth As String = Now.Month.ToString



Posted By: davidshq
Date Posted: 07 July 2004 at 1:40pm

     ' Find today's date so it can be compared against data.
     Dim TodaysDay as String = Now.Day.ToString
     Dim TodaysMonth as String = Now.Month.ToString
Gives error:

Name 'Now' is not declared.
Respectfully,
David.



-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.


Posted By: Mart
Date Posted: 07 July 2004 at 1:55pm

Try this:

Dim TodaysDay as String = DateTime.Now.Day.ToString
     Dim TodaysMonth as String = DateTime.Now.Month.ToString



Posted By: davidshq
Date Posted: 07 July 2004 at 2:05pm

Thanks Mart for your help. Its appreciated, however once again it threw an error...But a different one this time. Now its telling me:
No value given for one or more required parameters.
Here is the stack trace, I couldn't make any sense out of it:

[OleDbException (0x80040e10): No value given for one or more required parameters.]
    System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandli ng(Int32 hr) +41
    System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleRe sult(tagDBPARAMS dbParams, Object& executeResult) +122
    System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& ; executeResult) +92
    System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavio r behavior, Object& executeResult) +65
    System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(Command Behavior behavior, String method) +112
   System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) +69
   System.Data.OleDb.OleDbCommand.ExecuteReader() +7
   TodayEvents.Page_Load(Object sender, EventArgs e) +226
   System.Web.UI.Control.OnLoad(EventArgs e) +55
   System.Web.UI.Control.LoadRecursive() +27
   System.Web.UI.Page.ProcessRequestMain() +731



-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.


Posted By: Mart
Date Posted: 07 July 2004 at 2:29pm

You used 2 parameters in your SQL Query:

Select * from tblToday WHERE TodaysDay=mDay and TodaysMonth=mMonth

(mDay and mMonth)

You just need to add the value of the parameters to the OleDb command



Posted By: davidshq
Date Posted: 07 July 2004 at 2:40pm
When you say I need to add the value to the command, what exactly do you mean? mDay and mMonth have different values depending on which entry in the database they are?
Respectfully,
David.

-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.


Posted By: Mart
Date Posted: 07 July 2004 at 2:50pm

Sorry, just re-read your code.

You just did your SQL Statement a little wrong, heres what it should be:

DBReadData.CommandText = "Select * from tblToday WHERE mDay = " & TodaysDay & " and mMonth = " & TodaysMonth



Posted By: davidshq
Date Posted: 07 July 2004 at 2:53pm
Looks great, can't test it now as I have to run to my other job....But I definetly appreciate the help and will post back whether it works or not later. Thanks again.
Respectfully,
David.

-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.


Posted By: davidshq
Date Posted: 08 July 2004 at 11:19am
Mart,
   Thanks for your help. That worked. One last question. Is it possible to tell it to do an ORDER BY mYear ASC? I tried adding that command in several different places in the SQL statement and nothing seems to work.
   I tried things like:
SELECT * from tblToday ORDER BY mYear ASC WHERE...
SELECT * from tblToday WHERE... ORDER BY mYear
SELECT * from tblToday WHERE... AND ORDER BY mYear
   None of them worked. :-P I've done ORDER BY elsewhere, in the same format pretty much...but it just won't work here.
Respectfully,
David.

-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.


Posted By: Mart
Date Posted: 08 July 2004 at 11:22am
ORDER BY needs to be at the end of the query. Can you post in the full query and the error you get?


Posted By: davidshq
Date Posted: 08 July 2004 at 11:43am

This is the current code, without the ORDER BY statement.
DBReadData.CommandText = "Select * from tblToday WHERE mDay=" & TodaysDay & " and mMonth=" & TodaysMonth
Here is the code with ORDER BY:
DBReadData.CommandText = "Select * from tblToday WHERE mDay=" & TodaysDay & " and mMonth=" & TodaysMonth & "ORDER BY mYear, mMonth, mDay ASC"
The error message is as follows:
Syntax error (missing operator) in query expression 'mDay=8 and mMonth=7ORDER BY mYear, mMonth, mDay ASC'.
Respectfully,
David.



-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.


Posted By: Mart
Date Posted: 08 July 2004 at 12:15pm
You have just missed a space, it should be:

DBReadData.CommandText = "Select * from tblToday WHERE mDay=" & TodaysDay & " and mMonth=" & TodaysMonth & " ORDER BY mYear, mMonth, mDay ASC"


Posted By: davidshq
Date Posted: 08 July 2004 at 2:06pm
Works beautifully, thanks.
Respectfully,
David.

-------------
- http://www.davemackey.net/" rel="nofollow - Dave Mackey - Virtual Home.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net