seph
seph

Reputation: 694

Converting a String to a Date -> FormatException

Im desperately trying to convert the following String to a Date. I did research on how to do this, but was not able to do it successfully.

So basically, I have two Datepicker-Controls on my form. When the form is being saved, I always want to have the Date from Control #1, but keep the Time from Control #2. I am trying as following:

Dim strDatum As String = dtpDatum.Value.ToString() '= 03.01.13 00:00:00
Dim strStart As String = dtpVon.Value.ToString()   '= 23.01.13 10:39:01
Dim sStartDatum = Strings.Left(strDatum, 8) & " " & Strings.Mid(strStart, 10, 9) '= 03.01.13 10:39:01

'here I'm trying to convert my new String to a Date and assign it to my a parameter
.Parameters("@dStartZeit").Value = DateTime.ParseExact(sStartDatum, "DD.MM.YY HH:mm:ss", New Globalization.CultureInfo("en-US"))

I get an "FormatException" when executing my query.

What am I doing wrong here?

Thanks!

Upvotes: 0

Views: 137

Answers (2)

Muhammad Hani
Muhammad Hani

Reputation: 8664

formating strings requires case-sensitivity.

parameter.Parameters("@dStartZeit").Value = DateTime.ParseExact(sStartDatum, "dd.MM.yy HH:mm:ss", New Globalization.CultureInfo("en-US"))

Upvotes: 0

Bennor McCarthy
Bennor McCarthy

Reputation: 11675

Custom format strings are case-sensitive. Try:

DateTime.ParseExact(sStartDatum, "dd.MM.yy HH:mm:ss", New Globalization.CultureInfo("en-US")`

Upvotes: 1

Related Questions