Wrox Programmer Forums
|
ASP.NET 1.x and 2.0 Application Design Application design with ASP.NET 1.0, 1.1, and 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.x and 2.0 Application Design section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 25th, 2004, 08:07 AM
Authorized User
 
Join Date: Oct 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default Casting Problem

I have this code

SUB Page_Load

Dim strMyDate = Request.Form("textMyDate")
Dim DaMyd As DateTime = CDate(strMyDate )

END SUB

"textMyDate" is an <asp:Textbox> which I put in it somthing like
"14/11/2004"

But I got this Error:

Exception Details: System.InvalidCastException: Cast from string "14/11/2004" to type 'Date' is not valid.

What is the problem??

 
Old November 26th, 2004, 02:12 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hello,
for converting strings to DateTime you should use Parse method of DateTime,
Code:
DateTime dateObj = DateTime.Parse(stringVariable);
be carefull about IFormatProvider.

_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
 
Old January 2nd, 2007, 03:31 AM
Registered User
 
Join Date: Jun 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to bhat_bijoy
Default

I have find another problem when date is chicking sequentially in itemdatabound event of a datagrid. code is like this..
If CDate(szStr).DayOfWeek = DayOfWeek.Friday Then
                                lbl1.Text = "<b>" + CDate(szStr).ToString("MMM dd") + "<BR>" + Mid(CDate(szStr).DayOfWeek.ToString(), 1, 2) + "<b>"
                            Else
                                lbl1.Text = CDate(szStr).ToString("MMM dd") + "<BR>" + Mid(CDate(szStr).DayOfWeek.ToString(), 1, 2) + ""
                            End If

but when date string is "29/2/2006" then its showing an error....
Run-time exception thrown : System.InvalidCastException - Cast from string "2006/2/29" to type 'Date' is not valid.
I can i chek this date...if it not valid then i should go for next loop.






Similar Threads
Thread Thread Starter Forum Replies Last Post
casting LONG jomet Oracle 1 March 25th, 2008 12:43 AM
Casting pfrigo BOOK: Professional .NET 2.0 Generics 3 November 19th, 2007 04:49 PM
Casting - Can you help koco C# 1 September 24th, 2007 08:41 AM
casting ravibodani Access 1 April 4th, 2006 01:19 PM
Dynamic Casting pareshsoft C# 2 December 1st, 2004 12:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.