String was not recognized as a valid DateTime

monfu

Member
Joined
Jul 2, 2004
Messages
8
Location
Malta
Programming Experience
3-5
Dear All,

I have this piece of code:-

CType(r.FindControl("calDate"), Calendar).SelectedDate = DateTime.Parse(CType(r.FindControl("txtEvDate"), TextBox).Text())

And its giving me an error:-

String was not recognized as a valid DateTime

How can I solve this?

Thanks for your help and time

Johann
 
try this

hi,
try this code,
change ur parse statement into
"DateTime.Parse(cdate(CType(r.FindControl("txtEvDate"), TextBox).Text)"
u can also add the validation for this, ie)
dim TmpDate as Date
If isDate(CType(r.FindControl("txtEvDate"), TextBox).Text) Then
TmpDate=DateTime.Parse(cdate(CType(r.FindControl("txtEvDate"), TextBox).Text)
CType(r.FindControl("calDate"), Calendar).SelectedDate = TmpDate
Else
MsgBox.Show("Please enter date in correct format")
EndIF
 
Hi Jeevi

I solved it like this

Dim myDate As DateTime = DateTime.ParseExact(CType(r.FindControl("txtEvDate"), TextBox).Text(), "MM/dd/yy", New DateTimeFormatInfo)
'Dim myDate As Date = Date.ParseExact(CType(r.FindControl("txtEvDate"), TextBox).Text(), "dd/MM/yyyy", Nothing)
CType(r.FindControl("calDate"), System.Web.UI.WebControls.Calendar).SelectedDate = myDate.ToShortDateString

With the System.Globalization method

Thanks
 
Hi All,

I was doing some testing and needed to change a string to a date Below is how I did it. I am sure it will work with time as well.

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
' Create a new DateTimePicker
Dim dateTimePicker1 As New DateTimePicker()
Dim dtDate As Date
Dim strDate As String

strDate = "3/12/2006"
strDate = "#" + strDate + "#"
dtDate = strDate

dateTimePicker1.Value = dtDate
MessageBox.Show(dateTimePicker1.Value.ToString())
dateTimePicker1.Value = dtDate.AddDays(14)
MessageBox.Show(dateTimePicker1.Value.ToString())
End Sub

I am Pleased to be able to make even this small contribution the forum. I has helped me way more than I will ever be able to pay back. Thank you all for you contributions.

Hal


 
Back
Top