troubleshooting Question

How to pass a Date() object to a C# WebMethod?

Avatar of itnifl
itniflFlag for Norway asked on
ASP.NETWeb DevelopmentJSON
2 Comments1 Solution276 ViewsLast Modified:
function addBooking(logonName, startTime, endTime) {
    var hours = Math.abs(startTime - endTime) / 36e5;
        type: "POST",
        url: "/Sections/booker.aspx/BookTime",
        data: "{bookingDate= '" + startTime + "', durationHours=" + hours + ", logonName='" + logonName + "'}",
        contentType: "application/json; charset=utf-8;",
        dataType: "json",
        success: function (response) {
            if ($.parseJSON(response.d).runStatus) {
        error: function (xhr, status, error) {
            var err = xhr.responseText;
            alert('Error: ' + err);

Sending to this method:
      public static string BookTime(DateTime bookingDate, int durationHours, string logonName) {

Gives me the error:

"{\"Message\":\"Invalid object passed in, \\u0027:\\u0027 or \\u0027}\\u0027 expected. (13): {bookingDate= \\u0027Mon Nov 30 06:30:00 UTC+0100 2015\\u0027, durationHours=0.5, logonName=\\u0027Admin\\u0027}\",\"StackTrace\":\"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)\\r\\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\\r\\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\\r\\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\\r\\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\\r\\n   at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\\r\\n   at System.Web.Script.Services.RestHandler.GetRawParams
(WebServiceMethodData methodData, HttpContext context)\\r\\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)\",\"ExceptionType\":\"System.ArgumentException\"}"

So first I wonder if I am sending over the date object correctly?
Alexandre Simões
Software Architect

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros