none
500 internal server error - JQuery Ajax call to Wcf data service

    Question

  • Hi,

    I have the following data model on service side -

    public class X

        {

            public int x_id { get; set; }

            public string name { get; set; }

            public DateTime create_dt { get; set; }

            public List<Y> ylist { get; set; }

        }

    I am trying to do simple insert using HTTP Post in JQuery Ajax as follws but I get a 500 error.

     var data = { name: $("#name").val()};

     

                // JSONify the data

                var data = JSON.stringify(data);

     

                // Post it

                $.ajax({

                    type: "POST",

                    contentType: "application/json; charset=utf-8",

                    url: "http://localhost:3392/DataTest.svc/X",

                    data: data,

                    dataType: "json",

                    success: insertXCallback

                });

    As you can see X has other properties and is lnked to Y table for which I am not passing any data because I have set create_dt column on SQL Server to use getdate() by default when doing any row insert, and for Y I don't want to pass any data in this call. 

    How can I fix this error?

    Please advise

    mercredi 18 mai 2011 17:39

Toutes les réponses

  • It would help if you can get the exact error on the server side. Follow this post http://blogs.msdn.com/b/phaniraj/archive/2008/06/18/debugging-ado-net-data-services.aspx to get the call stack and message.

     

    Regards,

    PQ


    Peter Q. http://blogs.msdn.com/peter_qian
    mercredi 18 mai 2011 18:02
  • It would help if you can get the exact error on the server side. Follow this post http://blogs.msdn.com/b/phaniraj/archive/2008/06/18/debugging-ado-net-data-services.aspx to get the call stack and message.

     

    Regards,

    PQ


    Peter Q. http://blogs.msdn.com/peter_qian

    Thanks Peter. I found the problem but still looking for a solution. I replaced this 

    var data = { name: $("#name").val()};

    with the following:

    var data = { name: $("#campaignname").val(), create_dt: '2011-05-18 17:15:45' };

    and it worked. Meaning it wants the date in correct format.

    But I am setting this date in SQL Server anyways during the insert, so can I not pass null  or not pass this property at all

    Please advise.

     

     

    mercredi 18 mai 2011 18:06
  • Hi, What exactly is the error thrown? What is the shape of your metadata? The server should happily ignore missing properties in payload mainly because of projected entities will not upload the full payload. So as long as the field is totally missing then it'll be fine.
    Peter Q. http://blogs.msdn.com/peter_qian
    mercredi 18 mai 2011 18:34
  • Hi, What exactly is the error thrown? What is the shape of your metadata? The server should happily ignore missing properties in payload mainly because of projected entities will not upload the full payload. So as long as the field is totally missing then it'll be fine.
    Peter Q. http://blogs.msdn.com/peter_qian

    This is the exact error -

     

    <message>The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.&#xD;

     

    <?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
    <error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
     <code></code>
     <message xml:lang="en-US">An error occurred while processing this request.</message>
     <innererror>
      <message>An error occurred while updating the entries. See the inner exception for details.</message>
      <type>System.Data.UpdateException</type>
      <stacktrace>  at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)&#xD;
      at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)&#xD;
      at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)&#xD;
      at System.Data.Objects.ObjectContext.SaveChanges()&#xD;
      at System.Data.Services.Providers.ObjectContextServiceProvider.SaveChanges()&#xD;
      at System.Data.Services.UpdatableWrapper.SaveChanges()&#xD;
      at System.Data.Services.DataService`1.HandleNonBatchRequest(RequestDescription description)&#xD;
      at System.Data.Services.DataService`1.HandleRequest()</stacktrace>
      <internalexception>
       <message>The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.&#xD;
    The statement has been terminated.</message>
       <type>System.Data.SqlClient.SqlException</type>
       <stacktrace>  at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)&#xD;
      at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)&#xD;
      at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()&#xD;
      at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)&#xD;
      at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()&#xD;
      at System.Data.SqlClient.SqlDataReader.get_MetaData()&#xD;
      at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)&#xD;
      at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)&#xD;
      at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)&#xD;
      at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)&#xD;
      at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)&#xD;
      at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)&#xD;
      at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)&#xD;
      at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)&#xD;
      at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)</stacktrace>
      </internalexception>
     </innererror>
    </error>

    mercredi 18 mai 2011 18:48
  • Hi, What exactly is the error thrown? What is the shape of your metadata? The server should happily ignore missing properties in payload mainly because of projected entities will not upload the full payload. So as long as the field is totally missing then it'll be fine.
    Peter Q. http://blogs.msdn.com/peter_qian

    This is the problem and apparently i haven't founda way to resolve this other than changing my databases's column data type to datetime2, which I would hate to do.

     

    http://stackoverflow.com/questions/5365841/entity-framework-error-the-conversion-of-a-datetime2-data-type-to-a-datetime-dat

     

    Please suggest a solution to this.

    mercredi 18 mai 2011 19:09
  • Here Your Getting the Date Value as 01/01/0001 That's why you are Getting that Error. Once check Date Time Column value .
    mercredi 18 avril 2012 04:33