I have a strange problem in vb.net.
In this way, I have no problem converting a String to Date using ParseExact, some date format and CultureInfo:
Dim f As String = "30-mar-2012"
' Value of f is "30/03/2012"
xdate = DateTime.ParseExact(
f,
"dd-MMM-yyyy",
System.Globalization.CultureInfo.InvariantCulture)
Problem begins when using a DataReader
While dr.Read
Dim f As String = String.Format("{0:dd-MMM-yyyy}", dr.Item("fec_estado_insc")))
' Value of f is "30-mar-2012"
xdate = DateTime.ParseExact(
f,
"dd-MMM-yyyy",
System.Globalization.CultureInfo.InvariantCulture)
End While
Get error: "String was not recognized as a valid DateTime "
As you see, variable f is a string, its value comes from a row of a datareader, but is stil a string.
How is possible this?
EDIT
I found a hack way to get date in "dd-MMM-yyyy" format with Chan's help. I had to create this function:
Public Shared Function Giveme_Date_dd_MMM_yyyy(ByVal XobjValue As Object) As Nullable(Of Date)
If XobjValue Is System.DBNull.Value Then
Return Nothing
ElseIf XobjValue Is Nothing Then
Return Nothing
ElseIf XobjValue.ToString.Trim.Equals("") Then
Return Nothing
End If
Dim f As String = String.Format("{0:dd-MMM-yyy}", XobjValue)
Dim dtResult As Date, xdate As Date
If DateTime.TryParse(f, dtResult) Then
xdate = CDate(dtResult.ToString("f", System.Globalization.CultureInfo.InvariantCulture))
End If
Return xdate
End Function
And in database, had to change SELECT query in this way:
OPEN r_cursor FOR
SELECT to_char(fec_estado_insc,'dd/mm/yyyy') AS fec_estado_insc,
blah
blah
blah
However, original problem persists. Some vb.net bug?
Dim f As String = CStr(String.Format("{0:dd-MMM-yyy}", dr.Item("fec_estado_insc")).Clone())- вʀaᴎᴅᴏƞ вєнᴎєƞf, it's a string. A string is a string irrespective of how it was set or sourced; it won't affect your next line. You are doing something wrong... just try stepping thru the code line by line and inspecting the variable values. - Pradeep Kumar