User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 422,677 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,581 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser: Programming Forums
Views: 3361 | Replies: 4 | Solved
Reply
Join Date: Jan 2008
Posts: 24
Reputation: suganzeni is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
suganzeni suganzeni is offline Offline
Newbie Poster

Type cast in vb.net

  #1  
Mar 13th, 2008
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)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,300
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 455
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Type cast in vb.net

  #2  
Mar 13th, 2008
The Date type has a Parse method that will try to convert a string to a date:
  1. dim s_date1 as string
  2. dim s_date as date
  3. s_date1 = s_mon & "/" & s_day & "/" & s_year
  4. s_date = Date.Parse ( s_date1 )
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Jan 2008
Posts: 24
Reputation: suganzeni is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
suganzeni suganzeni is offline Offline
Newbie Poster

Re: Type cast in vb.net

  #3  
Mar 18th, 2008
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)
Last edited by suganzeni : Mar 18th, 2008 at 2:04 am.
Reply With Quote  
Join Date: Jan 2008
Posts: 24
Reputation: suganzeni is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
suganzeni suganzeni is offline Offline
Newbie Poster

Re: Type cast in vb.net

  #4  
Mar 18th, 2008
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
Reply With Quote  
Join Date: Sep 2004
Posts: 6,300
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 455
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Type cast in vb.net

  #5  
Mar 18th, 2008
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" )
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb VB.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the VB.NET Forum

All times are GMT -4. The time now is 4:42 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC