Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used this code snippet ;

C#
public IEnumerable<StdntInf> GetStdntInf()
    {
        int maxid = db.StdntInf.Max(m => m.StdntInfID);

        var sI = from n in db.StdntInf
                     where n.StdntInfID == maxid
                     select n;
        return sI;

    }

Exception:

JSON
{"Message":"An error has occurred.","ExceptionMessage":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Self referencing loop detected for property 'StdntInf' with type 'System.Data.Entity.DynamicProxies.StdntInf_8C7B5964BE0BCF23536487B4E4548AFAE3B31264D236BC395DE2120A20CA0E2D'. Path '[0].YktNmns[0]'.","ExceptionType":"Newtonsoft.Json.JsonSerializationException","StackTrace":"   konum: Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n   konum: Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n   konum: Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n   konum: Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n   konum: Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n   konum: Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n   konum: Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n   konum: Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n   konum: Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n   konum: Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value)\r\n   konum: Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)\r\n   konum: Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)\r\n   konum: System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.<WriteToStreamAsync>b__c()\r\n   konum: System.Threading.Tasks.TaskHelpe``rs.RunSynchronously(Action action, CancellationToken token)"}}
Posted

Try this code...

C#
var qry = db.StdntInf
                     .OrderByDescending(m => m.StdntInfID) 
                     .FirstOrDefault(); 
 
Share this answer
 
Comments
seydost 19-Feb-15 5:43am    
var id = db.StdntInf.ToList().OrderByDescending(p => p.StdntInfID);
var nt = id.Select(p => new StdntInf()
{
StdntInfiID = p.StdntInfID,
x= p.x,
y= p.y,
z= p.z,
t= p.t,
m = p.m,
v= p.v,
});
return nt;
I tried this code,but get all data
manak chand 19-Feb-15 6:16am    
.FirstOrDefault() is missing...

var id = db.StdntInf.ToList().OrderByDescending(p => p.StdntInfID).FirstOrDefault();
seydost 19-Feb-15 6:38am    
OK.I tried.But;
Error 2 'Tutanak.WebAPI.StdntInf' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'Tutanak.WebAPI.StdntInf' could be found (are you missing a using directive or an assembly reference?) C:\Users\seydabenli\Documents\Visual Studio 2012\Projects\Tutanak.WebAPI\Tutanak.WebAPI\Controllers\StdntInfController.cs 65 22 Tutanak.WebAPI
Refer - Failed to serialize the response in Web API with Json[^].
Quote:
When it comes to returning data back to the consumer from Web Api (or any other web service for that matter), I highly recommend not passing back entities that come from a database. It is much more reliable and maintainable to use Models in which you have control of what the data looks like and not the database.
 
Share this answer
 
Comments
seydost 19-Feb-15 5:05am    
I am using Entitiy Framework !
The concept is, you should not send the entity directly. You should send a Class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900