Proper DateTime Format for a Web Service
Asked Answered
M

4

7

I have a webservice with a method which is called via a xmlhttprequest object in my javascript. The method accepts a datetime parameter which is subsequently converted to a string and run against the database to perform a calculation.

I get the value from m_txtDateAdd and send off the xmlHttprequest

<asp:textbox id=m_txtDateAdd tabIndex=4 runat="server" Width="96px" Text="<%# Today %>">
</asp:textbox>

which has a validator attacted to it

<asp:CustomValidator id="m_DateAddValidator" runat="server" ErrorMessage="Please Enter a Valid Date" ControlToValidate="m_txtDateAdd">&#x25CF;</asp:CustomValidator>

My webmethod looks something like this

[WebMethod]
public decimal GetTotalCost(DateTime transactionDate)
{
    String sqlDateString = transactionDate.Year+"/"+transactionDate.Month+"/"+transactionDate.Day;

I use sqlDateString as part of the commandtext i send off to the database. Its a legacy application and its inline sql so I don't have the freedom to set up a stored procedure and create and assign parameters in my code behind. This works 90% of the time. The webservice is called on the onchange event of m_txtDateAdd. Every now and again the response i get from the server is

System.ArgumentException: Cannot convert 25/06/2009 to System.DateTime. System.ArgumentException: Cannot convert 25/06/2009 to System.DateTime.

Parameter name: type ---> System.FormatException: String was not recognized as a valid DateTime.

   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at System.DateTime.Parse(String s, IFormatProvider provider)
   at System.Convert.ToDateTime(String value, IFormatProvider provider)
   at System.String.System.IConvertible.ToDateTime(IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
   --- End of inner exception stack trace ---
   at System.Web.Services.Protocols.ScalarFormatter.FromString(String value, Type type)
   at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)
   at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
Mecham answered 10/7, 2009 at 14:42 Comment(2)
Can you show the custom validator code? The serializer will attempt to convert your string using default formatting which can only understand a few basic formats. The validator should ensure that a known format is being submitted.Renewal
Its a standard asp.net custom validator. I haven't written my own oneMecham
R
11

You need to send a DateTime in the correct format for XML: 2009-07-10T12:40Z. From http://en.wikipedia.org/wiki/ISO_8601.


Also, why are you using HttpRequest? Why not just use Add Service Reference?

Ryeland answered 10/7, 2009 at 14:43 Comment(4)
There is no "correct format for XML", but there is a correct format for XML Schema. Careful not to conflate the two. If the consumer of the XML is a javascript client, the ISO date format may not be useful.Karyosome
His service accepts DateTime. XML Serializer always uses XSD formats to deserialize and serialize, unless IXmlSerializable is implemented, which it's not on DateTime.Ryeland
An important thing to remember that I said data is returned sucessfully 90%+ of the time. Repeatedly changing the date will eventually yield the exception but there is no consistency in that say entering 01/07/2009 over and over will eventually cause it to fail. using xmlhttprequest so you don't get a full page refreshMecham
I said instead of using a script reference, but ok. BTW, if that's the stack trace you get, then you need to figure out where in the chain between the client and the service somebody is screwing with your messages. The format isn't changing on its own.Ryeland
A
5

2001-10-26T19:32:52Z

Use such format.

Ancalin answered 1/8, 2012 at 18:41 Comment(0)
O
0

How are you creating the DateTime to pass into GetTotalCost()?

Have you got the correct constructor overload from http://msdn.microsoft.com/en-us/library/system.datetime.aspx?

Opposite answered 10/7, 2009 at 15:11 Comment(0)
P
0

Are you passing the String value from the text box directly to the webservice? It would be more reliable to parse the user input String into a javascript Date object, pass the Date object to the webservice, and let the serializer in Microsoft's ajax library figure out how to format it.

It may be helpful to see the client side javascript code that is getting the value and calling the web service.

Prot answered 30/3, 2010 at 18:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.