1
votes

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?

2
Maybe try this? Dim f As String = CStr(String.Format("{0:dd-MMM-yyy}", dr.Item("fec_estado_insc")).Clone()) - вʀaᴎᴅᴏƞ вєнᴎєƞ
Get same error: "String was not recognized as a valid DateTime ". It' strange. If I set directly variable f = "30-mar-2012", there is no error. But error appears when filling f value from DataReader. - Delmonte
Once you have set the value of 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

2 Answers

0
votes

Are you absolutely sure the data is datetime? What if it's null? How about an additional check?

Dim f As String = "30-mar-2012"
Dim dtResult As Date, xdate As Date

If DateTime.TryParse(f, dtResult) Then
    xdate = dtResult.ToString("f", System.Globalization.CultureInfo.InvariantCulture)

End If
0
votes

You can avoid the whole problem by storing the Date as a Date, then reading it back as a Date. Dates dont have a format, they are just a value, but you can display them any way you want using format.

ID Name RealDate StringDate
1 Test1 3/30/2012 30-mar-2012

The date can be read back as a Date this way:

' "SELECT Name, RealDate, StringDate FROM...
Using rdr = cmd.ExecuteReader()
    rdr.Read()
    realDate = rdr.GetDateTime(1)

    Console.WriteLine("realdate type: {0}  value: {1} ", realDate.GetType, realDate)
    Console.WriteLine(realDate.ToString("dd-MMM-yyyy"))

Unfortunately, the type reader methods only work off an ordinal index, but returns the value as a Date. The output:

realdate type: System.DateTime value: 3/30/2012 12:00:00 AM
30-Mar-2012

If the DB col is a Date type, the DB is only going to accept a Date type into the column. To read it by column name you can parse or just convert:

realDate = Convert.ToDateTime(rdr("realdate").ToString)

When you "must" convert from string (assumes the "date" is "dd-MMM-yyyy"):

stringDate = rdr("stringDate").ToString
Console.WriteLine("String Date date: {0}", stringDate)
Dim newDate = DateTime.ParseExact(stringDate, "dd-MMM-yyyy", CultureInfo.InvariantCulture)
Console.WriteLine("String Date reparsed: {0}", newDate)

Output:

String Date date: 30-mar-2012
String Date reparsed: 3/30/2012 12:00:00 AM

If the data is in the database in some known data format, convert/parse it to date then try to apply a format. On one piece of code, you are using String to do DateTime formatting and acting on Object (dr.Item returns object). There is no Date type involved.