DarkW1nter
DarkW1nter

Reputation: 2861

C# web service returns invalid cast for an oracle date-time

Im looping through a ref cursor returned from a webservice, I have a class for the object I want to finally return as a list, and Im adding each record from the cursor to this list. One of the fields in the class should be DateTime:

[DataMember(Order = 5)]
    public DateTime StartDate
    {
        get;
        set;
    }

but I get an invalid cast error when I execute this line within my read loop

pe.StartDate = reader.GetDateTime(4);

If i change the type in the class to string and execute GetString instead it works, returning dd/mm/yyyy, but I would rather return the proper Oracle date-time. Any hints as to how I can get this to work?

thanks

Upvotes: 0

Views: 171

Answers (1)

Roy Dictus
Roy Dictus

Reputation: 33149

Read it as a string, then convert it to the proper value:

pe.StartDate = DateTime.Parse(reader.GetString(4), new CultureInfo("fr-FR"));

Of course you can substitute the French culture here for any culture where "dd/mm/yyyy" is the standard date format.

Upvotes: 1

Related Questions