0

Hi all when i trying to convert a string to date and store in data base
it shows type cast error

code is as follows:

dim s_date1 as string
dim s_date as date
s_date1 = s_mon & "/" & s_day & "/" & s_year
s_date = CType(s_date1, Date)

2
Contributors
4
Replies
6
Views
8 Years
Discussion Span
Last Post by Narue
0

The Date type has a Parse method that will try to convert a string to a date:

dim s_date1 as string
dim s_date as date
s_date1 = s_mon & "/" & s_day & "/" & s_year
s_date = Date.Parse ( s_date1 )
0

HI
dim s_date1 as stringdim s_date as dates_date1 = s_mon & "/" & s_day & "/" & s_years_date = Date.Parse ( s_date1 )dim s_date1 as string
dim s_date as date
s_date1 = s_mon & "/" & s_day & "/" & s_year
s_date = Date.Parse ( s_date1 )

After i use the above code i got the following error

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: FormatException

also i tried with following types of type cast from string to date
s_date = Convert.ToDateTime(s_date1)
s_date = Date.ParseExact(s_date1, "mm/dd/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
s_date = DateTime.Parse(s_date1)

0

Hi
I found whats the problem.
The format exception is because i have given it as
s_date1 = s_mon & "/" &s_day & "/" & s_year
But the system format is (dd/mm/yyyy)
so after giving as like follows
s_date1 = s_day & "/" & s_mon & "/" & s_year

All type casting form string to date is working.
s_date = CType(s_date1, Date)
s_date = Date.Parse(s_date1)
Its just a simple mistake.
Because of not clear about the date format of the system.
Any way thanks for your help NARUE

0

Right, the parse format is by default the system format. You can get around that incompatibility by constructing a date directly and then converting it to a date string with your desired format (if you need the string for anything else):

s_date = New Date ( s_year, s_mon, s_day )
s_date1 = s_date.ToString ( "MM/dd/yyyy" )
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.