Click here to Skip to main content
15,747,637 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I try to call storerprocedure in LINQ but this show error

What I have tried:

public static string search_data(DateTime fromdate, DateTime todate, string region)
string result = "";

Ts1 ts = new Ts1();
List<ts1> dq = ts.griddataresult(fromdate, todate, region).ToList();

DataTable dt = new DataTable();

dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Owner", typeof(string));

foreach (var c in dq)
dt.Rows.Add(c.ID, c.owner);

result = DataSetToJSON(dt);
return result;
catch (Exception)
throw new Exception();

public static string DataSetToJSON(DataTable dt)
Dictionary<string,> dict = new Dictionary<string,>();
object[] arr = new object[dt.Rows.Count + 1];

for (int i = 0; i <= dt.Rows.Count - 1; i++)
arr[i] = dt.Rows[i].ItemArray;

dict.Add("response", arr);

JavaScriptSerializer json = new JavaScriptSerializer();
return json.Serialize(dict);
Updated 25-Jul-16 2:09am
Suvendu Shekhar Giri 25-Jul-16 3:12am    
That's because "dq" is not a collection and to use foreach, you need a collection.
super_user 25-Jul-16 7:59am    
check update @Suvendu Shekhar Giri

This is one of the reasons you should avoid using "var" until you're more experienced with .net. EF thinks your SP is what is known as a scalar procedure, ie it only returns a single value, not a result set\table. So the return value is int so your dq variable is int, and you can only loop on variables that are collections. Have a look at how the SP is create in EF, maybe delete it from the EF model and re-add it.
Share this answer
super_user 25-Jul-16 5:30am    
i also re add this..

so what should i use instead of var
F-ES Sitecore 25-Jul-16 5:37am    
Use the proper type. "var" is just a replacement for an actual type, but it means by looking at your code you don't know what the type of anything is, and when you post your code on-line people looking at your code don't know either.
super_user 25-Jul-16 6:00am    
type of return data?
super_user 25-Jul-16 7:56am    
@F-ES Sitecore check this image i set return type int ..
F-ES Sitecore 25-Jul-16 8:06am    
That's your issue then, if the SP does not return an int you need to configure it such that it returns the right thing. Not sure what it returns, but you could try the "Get Column Information" button to see if it works, or select the relevant type from the entities dropdown if a suitable entity exists.

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