Click here to Skip to main content
15,883,744 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys, I have a function with store procedure entity framework, i want to return as JSON string .
C#
public static string GetGroupModFunc(string group_mod_id)
       {

           var idParam = new SqlParameter
           {
               ParameterName = "GID",
               Value = group_mod_id
           };
           var obj= db.Database.SqlQuery<string>("EXEC GET_GROUP_PERMIT @GID", idParam).FirstOrDefault();
           string json = "";
           return json = JsonConvert.SerializeObject(obj);
       }
Posted

you have .FirstOrDefault(); at the end of your c# statement and it will give only single item or null value as result. if you need array or list, use ToList or ToArray methods
C#
var obj= db.Database.SqlQuery<string>("EXEC GET_GROUP_PERMIT @GID", idParam).ToList();
return JsonConvert.SerializeObject(obj);
 
Share this answer
 
v2
Comments
headshot9x 17-May-15 23:52pm    
Well, when debug at var obj = db.Database.SqlQuery<string>("EXEC GET_GROUP_PERMIT @GID", idParam).ToList();
return JsonConvert.SerializeObject(obj); . It's show error "The data reader has more than one field. Multiple fields are not valid for EDM primitive or enumeration types".
change the code like this


List<columns> data = objectContext.ExecuteStoreQuery<columns>("EXEC GET_GROUP_PERMIT @GID", idParam).AsQueryable().ToList();</columns></columns>
 
Share this answer
 
v2
Comments
headshot9x 18-May-15 3:30am    
Hi Santhosh Babu Mahimairaj. It's not work , I have see ref http://stackoverflow.com/questions/23910390/the-data-reader-has-more-than-one-field-multiple-fields-are-not-valid-for-edm-p ago, but it's not still work.
I found solution for this issue : - Get data of store to DataTable , then conver data from DataTable to JSON. It's work fine.
 
Share this answer
 

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