Hi,
I'm working through "SAMS ASP.Net in 21 Days", and have a problem with the
exercise on page 93, for which even the online source code doesn't work!
This code is below, and it produces an error connected to the DateTime
variable - and I don't know why.
Can anyone explain why it doesn't work?
Thanks!
JON
ERROR MESSAGE
Cast from String ('06/27/78') to Date is not valid.
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.InvalidCastException: Cast from String
('06/27/78') to Date is not valid.
Source Error:
Line 4: public dtBirthDay as DateTime = "06/27/78"
ORIGINAL CODE
<%@ Page Language="VB" %>
<script runat="server">
Class Chris
public dtBirthDay as DateTime = "06/27/78"
public strHairColor as string = "Brown"
public strEyeColor as string = "Brown"
function GetDayOfWeek()
dim strDay as string
select case dtBirthDay.DayOfWeek
case 1
return "Sunday"
case 2
return "Monday"
case 3
return "Tuesday"
case 4
return "Wednesday"
case 5
return "Thursday"
case 6
return "Friday"
case 7
return "Saturday"
end select
end function
End Class
sub Button_Click(obj as object, e as eventargs)
dim objChris as new Chris
Response.write(objChris.GetDayOfWeek)
end sub
</script>
<html>
<body>
<form runat="server">
<asp:button id="btSubmit" Text="Submit"
runat="server" OnClick="Button_Click" />
<p>
</form>
</body>
</html>
This is because you are try to initailize the date variable as a string date
type. try using the CDate function to convert the date first. Avoid using
wromg syntax.
Fred Musau
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com