By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,561 Members | 3,083 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,561 IT Pros & Developers. It's quick & easy.

Date Errors in .NET 2.0

P: n/a
Here is my code:

*****

Public Property DOB() As Date
Get
Try

DOB = (msBirthMonth + "/" + msBirthDay + "/" +
msBirthYear)

Catch e As Exception

DOB = Nothing
End Try
End Get
Set(ByVal d As Date)
Try
msBirthMonth = Month(d).ToString
msBirthDay = Day(d).ToString
msBirthYear = Year(d).ToString
Catch ex As Exception
msBirthMonth = ""
msBirthDay = ""
msBirthYear = ""
End Try
End Set

End Property

****
values passed are:
msBirthMonth = '01'
msBirthDay = '01'
msBirthYear = '1950'

***

The error that I get is, "Conversion from String "01/01/1950" to type
date is not valid"

This code worked in .NET 1.1, now that I'm converting to .NET 2.0, this
code no longer works.

Any suggestions?

May 8 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Milissa,
Consider using a Date constructor instead:

| DOB = (msBirthMonth + "/" + msBirthDay + "/" +
| msBirthYear)

Return New Date(CInt(msBirthYear), CInt(msBirthMonth), CInt(msBirthDay))

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
<me**********@gmail.com> wrote in message
news:11*********************@y43g2000cwc.googlegro ups.com...
| Here is my code:
|
| *****
|
| Public Property DOB() As Date
| Get
| Try
|
| DOB = (msBirthMonth + "/" + msBirthDay + "/" +
| msBirthYear)
|
| Catch e As Exception
|
| DOB = Nothing
| End Try
| End Get
| Set(ByVal d As Date)
| Try
| msBirthMonth = Month(d).ToString
| msBirthDay = Day(d).ToString
| msBirthYear = Year(d).ToString
| Catch ex As Exception
| msBirthMonth = ""
| msBirthDay = ""
| msBirthYear = ""
| End Try
| End Set
|
| End Property
|
| ****
| values passed are:
| msBirthMonth = '01'
| msBirthDay = '01'
| msBirthYear = '1950'
|
| ***
|
| The error that I get is, "Conversion from String "01/01/1950" to type
| date is not valid"
|
| This code worked in .NET 1.1, now that I'm converting to .NET 2.0, this
| code no longer works.
|
| Any suggestions?
|
May 8 '06 #2

P: n/a
Ok, I tried returning the new date as you suggested - no luck...
I tried DOB = new date(...) no luck...
I tried dim mydate as date - put values in there and then dob = mydate
.... no luck

Setting a date property should be easier than this... Had the issue
been the fact that the string was originally in dd/mm/yyyy format,
converting the strings to integers should have resolved any of those
problems, but yet overriding the values and hard-coding the date value
should have resolved something also, which it still errors. I have been
stepping through debug, and msBirthMonth, ...day, ...year are passing
properly.

This is not the only date function that has errored on the conversion
to .NET 2.0 - ALL dates in the application are not returning
appropriate values. Instead all I get is #12:00:00 AM# for the dob
values in my watch.

These are simply drop down boxes on an ASPX page that stores their
values into birthmonth, birthyear and birthday. Those are then
converted by public properties to strings as msbirthmonth, msbirthday,
msbirthyear - then those string values are put in the date...

May 9 '06 #3

P: n/a
Liz

<me**********@gmail.com> wrote in message
news:11*********************@y43g2000cwc.googlegro ups.com...
Here is my code:

*****

Public Property DOB() As Date
Get
Try

DOB = (msBirthMonth + "/" + msBirthDay + "/" +
msBirthYear)

Catch e As Exception

DOB = Nothing
End Try
End Get
Set(ByVal d As Date)
Try
msBirthMonth = Month(d).ToString
msBirthDay = Day(d).ToString
msBirthYear = Year(d).ToString
Catch ex As Exception
msBirthMonth = ""
msBirthDay = ""
msBirthYear = ""
End Try
End Set

End Property

for one thing, DOB is declared as Date and your GET returns a string value
....
****
values passed are:
msBirthMonth = '01'
msBirthDay = '01'
msBirthYear = '1950'
values passed to what ? how ? your SET wants a single Date argument ...
you're passing it three strings ?

***

The error that I get is, "Conversion from String "01/01/1950" to type
date is not valid"

This code worked in .NET 1.1, now that I'm converting to .NET 2.0, this
code no longer works.


you were probably doing implicit conversions with Option Strict Off ... I
can't really follow your code or your intent; turning Option Strict OFF
might do the trick here but I would re-think the whole thing and get all the
data typing and logic straight

it looks like you want to pass a date value in and then return a string
value .... but then it doesn't .... it's confused; and the Try ... Catch in
your GET is suspect ... what is it you're doing there ? why not just control
the input ? and the purpose of setting DOB = Nothing is what ?


May 10 '06 #4

P: n/a
I'm fixing mumbled jumbled contractor code while converting from .NET
1.1 to .NET 2.0.

This was code that they origionally had a contractor do and on
conversion it all died! So as far as the why's, what for's... I have no
idea why they did half of anything to begin with.
values passed to what ? how ? your SET wants a single Date argument ...
you're passing it three strings ?
They passed string values from textboxes into variables, it's stored in
variables under applicant.dob.month, applicant.dob.day,
applicant.dob.year - and then they passed it into the msBirthMonth,
msBirthDay, msBirthYear ...and then changed to BirthMonth, BirthYear,
BirthDay later... then converted into a date, but it still uses string
values further in the application, but the end result is that it's
stored as a date in SQLServer... o.O
you were probably doing implicit conversions with Option Strict Off ... I
can't really follow your code or your intent; turning Option Strict OFF
might do the trick here but I would re-think the whole thing and get all the
data typing and logic straight
Thanks, I'll try that.
it looks like you want to pass a date value in and then return a string
value .... but then it doesn't .... it's confused; and the Try ... Catch in
your GET is suspect ... what is it you're doing there ? why not just control
the input ? and the purpose of setting DOB = Nothing is what ?


I can't even answer why they did that. Just that I have to fix all this
code. :|

May 10 '06 #5

P: n/a
Melissa Nava wrote:
I'm fixing mumbled jumbled contractor code while converting from .NET
1.1 to .NET 2.0.

This was code that they origionally had a contractor do and on
conversion it all died! So as far as the why's, what for's... I have no
idea why they did half of anything to begin with.


The problem isn't really the conversion from 1.1 to 2.0, but the fact
that the code is badly written to start with. (Apologies to the original
author for being so frank...)

It uses an implicit conversion from a string to date, which means that
it uses the current culture to decide how to parse the string. Unless
the application specifically sets the culture, the code is very
sensetive to changes in the environment.

For parsing the strings into a date you could use something like:

Dim m as Integer, d as Integer, y as Integer

If Integer.TryParse(msBirthYear, y) and Integer.TryParse(msBirthMonth,
m) and Integer.TryParse(msBirthDay, d) Then
DOB = New Date(y, m, d)
Else
DOB = Nothing
End If

[With reservations for errors - I don't usually program VB]
values passed to what ? how ? your SET wants a single Date argument ...
you're passing it three strings ?


They passed string values from textboxes into variables, it's stored in
variables under applicant.dob.month, applicant.dob.day,
applicant.dob.year - and then they passed it into the msBirthMonth,
msBirthDay, msBirthYear ...and then changed to BirthMonth, BirthYear,
BirthDay later... then converted into a date, but it still uses string
values further in the application, but the end result is that it's
stored as a date in SQLServer... o.O


As it is a date, it should really be handled as a date throughout the
application, not a bunch of strings.
you were probably doing implicit conversions with Option Strict Off ... I
can't really follow your code or your intent; turning Option Strict OFF
might do the trick here but I would re-think the whole thing and get all the
data typing and logic straight


Thanks, I'll try that.
it looks like you want to pass a date value in and then return a string
value .... but then it doesn't .... it's confused; and the Try ... Catch in
your GET is suspect ... what is it you're doing there ? why not just control
the input ? and the purpose of setting DOB = Nothing is what ?


I can't even answer why they did that. Just that I have to fix all this
code. :|

May 10 '06 #6

P: n/a
Melissa,
| Ok, I tried returning the new date as you suggested - no luck...
| I tried DOB = new date(...) no luck...
By "no luck" what do you mean, what is the specific error you are getting.
Where specifically are you seeing a problem & what specifically is the
problem. Are you certain (absolutely certain) that this routine is failing &
not another routine?
My code assumes that msBirthMonth, msBirthDay, and msBirthYear are strings
with numbers in them and are in the respective ranges. Göran's code will
ensure they are integers.

For example:
Dim msBirthMonth As String = "01"
Dim msBirthDay As String = "01"
Dim msBirthYear As String = "1950"

Dim aDate As Date = New Date(CInt(msBirthYear), CInt(msBirthMonth),
CInt(msBirthDay))

Sets the aDate variable to #1/1/1950#.

Moving from .NET 1.1 to .NET 2.0 would not (should not) affect your date
logic.
| Setting a date property should be easier than this...
It is!

Private m_dob

Public Property DOB() As Date
Get
Return m_dob
End Get
Set(ByVal value As Date)
m_dob = value
End Set

End Property

Is all the code for DOB really needs.

It really depends on how msBirthMonth, ...day, ...year are being used
elsewhere if they are actually needed or not...
| I have been
| stepping through debug, and msBirthMonth, ...day, ...year are passing
| properly.
Are you getting tripped up in PostBack? Remember a web page's object get
recreated each time they post back, if you set the DOB property when the
page is initially displayed, then attempt to use DOB in the post back (in
response to a button click for example) then msBirthMonth, ...day, ...year
(ergo DOB) will have reverted to blanks...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Melissa Nava" <me**********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
| Ok, I tried returning the new date as you suggested - no luck...
| I tried DOB = new date(...) no luck...
| I tried dim mydate as date - put values in there and then dob = mydate
| ... no luck
|
| Setting a date property should be easier than this... Had the issue
| been the fact that the string was originally in dd/mm/yyyy format,
| converting the strings to integers should have resolved any of those
| problems, but yet overriding the values and hard-coding the date value
| should have resolved something also, which it still errors. I have been
| stepping through debug, and msBirthMonth, ...day, ...year are passing
| properly.
|
| This is not the only date function that has errored on the conversion
| to .NET 2.0 - ALL dates in the application are not returning
| appropriate values. Instead all I get is #12:00:00 AM# for the dob
| values in my watch.
|
| These are simply drop down boxes on an ASPX page that stores their
| values into birthmonth, birthyear and birthday. Those are then
| converted by public properties to strings as msbirthmonth, msbirthday,
| msbirthyear - then those string values are put in the date...
|
May 10 '06 #7

P: n/a
Liz

"Melissa Nava" <me**********@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
I'm fixing mumbled jumbled contractor code while converting from .NET
1.1 to .NET 2.0.


oh. been there and done that ... I usually start over; it's faster and
you get better code ... provided that your "powers that be" can bear the
thought of throwing away the garbage they paid for ....


May 10 '06 #8

P: n/a
By "no luck" what do you mean, what is the specific error you are getting.
Where specifically are you seeing a problem & what specifically is the
problem. Are you certain (absolutely certain) that this routine is failing &
not another routine?
By "no luck" I mean that no matter how I try to set the DOB as a date,
I get the "Conversion from String "01/01/1950" to type date is not
valid" exception error thrown in the get statement.
| Public Property DOB() As Date
| Get
| Try
| DOB = (msBirthMonth + "/" + msBirthDay + "/" + msBirthYear)
| Catch e As Exception
| DOB = Nothing
| End Try
| End Get My code assumes that msBirthMonth, msBirthDay, and msBirthYear are strings
with numbers in them and are in the respective ranges. Göran's code will
ensure they are integers. For example:
Dim msBirthMonth As String = "01"
Dim msBirthDay As String = "01"
Dim msBirthYear As String = "1950"
Dim aDate As Date = New Date(CInt(msBirthYear), CInt(msBirthMonth),
CInt(msBirthDay))
Sets the aDate variable to #1/1/1950#.
In theory yes, this should work and if I put it in other areas of the
project, it works and returns an appropriate value. Inside of my
variableobject code within my Get/Set statement, this same code (that
works elsewhere) fails with an exception.
It really depends on how msBirthMonth, ...day, ...year are being used
elsewhere if they are actually needed or not...
They are used throughout the application for various purposes,
determining the age of an applicant to see if they are filing the
appropriate application, adding it to the database, checking for
various things such as if an applicant states that they were born in
1999, but had an absence during 1998, it will let them know that they
cannot report absences for a time before they were born...etc. So yes,
the DOB and different day, month, year values are used multiple times
throughout the application.
Are you getting tripped up in PostBack? Remember a web page's object get
recreated each time they post back, if you set the DOB property when the
page is initially displayed, then attempt to use DOB in the post back (in
response to a button click for example) then msBirthMonth, ...day, ...year
(ergo DOB) will have reverted to blanks...


I wish it was that easy... For DOB to revert to blank it would have to
have a value at some point. At this point I cannot get it to hold a
value at all. Stepping through debugger, it never even gets a value,
the values in the msBirthday, msBirthYear and msBirthMonth will hold
appropriate values. I have even tried using other values as the
application also holds birthdate information in regular Birthday,
BirthMonth and BirthYear... I have found a way to bypass the DOB
property, which will get me past the applications validations, error
handling, and let me continue through the application, but at the end
of the application since the DOB VO property is nothing it will error
on database save. (So If I don't fix it now, I can bypass this problem,
but eventually I have to figure it out. *bleh*)

I have encorporated the help of a Systems Programmer V here at my work
and this has him confused as well... Especially due to the fact that
the code is working if it is outside of the variable object.

Of course, thank you for your help, so far we have tried using
everyone's suggestions to no avail, but we definately know a dozen ways
that this won't work now! :)

May 10 '06 #9

P: n/a
Melissa,
Can you post or send me a "Short but Complete" sample that demonstrates the
problem?

For "Short but Complete" see:
http://www.yoda.arachsys.com/csharp/complete.html
If you can get it down to 15 to 20 lines that consistently cause the problem
feel free to post them here. If you have substantially more you may email
directly, this email address is not mangled...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Melissa Nava" <me**********@gmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
By "no luck" what do you mean, what is the specific error you are getting.
Where specifically are you seeing a problem & what specifically is the
problem. Are you certain (absolutely certain) that this routine is failing
&
not another routine?
By "no luck" I mean that no matter how I try to set the DOB as a date,
I get the "Conversion from String "01/01/1950" to type date is not
valid" exception error thrown in the get statement.
| Public Property DOB() As Date
| Get
| Try
| DOB = (msBirthMonth + "/" + msBirthDay + "/" +
msBirthYear)
| Catch e As Exception
| DOB = Nothing
| End Try
| End Get My code assumes that msBirthMonth, msBirthDay, and msBirthYear are strings
with numbers in them and are in the respective ranges. Göran's code will
ensure they are integers. For example:
Dim msBirthMonth As String = "01"
Dim msBirthDay As String = "01"
Dim msBirthYear As String = "1950"
Dim aDate As Date = New Date(CInt(msBirthYear),
CInt(msBirthMonth),
CInt(msBirthDay))
Sets the aDate variable to #1/1/1950#.
In theory yes, this should work and if I put it in other areas of the
project, it works and returns an appropriate value. Inside of my
variableobject code within my Get/Set statement, this same code (that
works elsewhere) fails with an exception.
It really depends on how msBirthMonth, ...day, ...year are being used
elsewhere if they are actually needed or not...
They are used throughout the application for various purposes,
determining the age of an applicant to see if they are filing the
appropriate application, adding it to the database, checking for
various things such as if an applicant states that they were born in
1999, but had an absence during 1998, it will let them know that they
cannot report absences for a time before they were born...etc. So yes,
the DOB and different day, month, year values are used multiple times
throughout the application.
Are you getting tripped up in PostBack? Remember a web page's object get
recreated each time they post back, if you set the DOB property when the
page is initially displayed, then attempt to use DOB in the post back (in
response to a button click for example) then msBirthMonth, ...day, ...year
(ergo DOB) will have reverted to blanks...


I wish it was that easy... For DOB to revert to blank it would have to
have a value at some point. At this point I cannot get it to hold a
value at all. Stepping through debugger, it never even gets a value,
the values in the msBirthday, msBirthYear and msBirthMonth will hold
appropriate values. I have even tried using other values as the
application also holds birthdate information in regular Birthday,
BirthMonth and BirthYear... I have found a way to bypass the DOB
property, which will get me past the applications validations, error
handling, and let me continue through the application, but at the end
of the application since the DOB VO property is nothing it will error
on database save. (So If I don't fix it now, I can bypass this problem,
but eventually I have to figure it out. *bleh*)

I have encorporated the help of a Systems Programmer V here at my work
and this has him confused as well... Especially due to the fact that
the code is working if it is outside of the variable object.

Of course, thank you for your help, so far we have tried using
everyone's suggestions to no avail, but we definately know a dozen ways
that this won't work now! :)
May 10 '06 #10

P: n/a
I put this thing aside for about a week worked on some other areas and
then returned to it about a half an hour ago.

The solution was very simple. A combination of suggestions:

DOB = new date(Cint(msbirthyear), Cint(msbirthmonth), Cint(msbirthday))
Return DOB

I knew it had to be simple... I thought I tried that to begin with, but
perhaps I didn't.

Thanks for everyone's help.

May 17 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.