Click here to Skip to main content
15,888,162 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i try to get result in highcharts through following code and also through store procedure

What I have tried:

C#
[WebMethod]
  public static string jqufunc(int years)
  {
    string res = "[";
      ProjectdbEntities a = new ProjectdbEntities();
      var b = a.Catg_type;
      foreach (var c in b)
      {

          res += "'" + c.Catg_type1 + "',";
      }
      res = res.Substring(0, res.Length - 1);
      res += "]";
      var allprogs = a.Program_type;
      string res2 = "[";
      foreach (var pr in allprogs)
      {
          res2 += "{name: '" + pr.Prog_name + "',";

          var y = a.Year_info;
          foreach (var g in y)
          {
              res2 += "'" + g.year + "',";


              //for category type
              var allcats = a.Catg_type;
             res2 += "data:[";
             foreach(var ab in result)
              {
                  res2 = res2 + ab.spstdinfo(year);
              }

              //foreach (var ct in allcats)
              //{
              //    res2 += a.Std_info.Where(t => t.Catg_id == ct.Catg_id && t.Prog_id == pr.Prog_id && t.year_id ==g.year_id).Count().ToString() + ",";

              //}

              res2 = res2.Substring(0, res2.Length - 1);
              res2 += "]";
              res2 += "},";
              return res + "*" + res2;
              }

      }
      return "";
  }


store procedure
SQL
ALTER procedure [dbo].[spstdinfo]
@year int
as
select Catg_type.Catg_type,Program_type.Prog_name ,Year_info.year,
COUNT(Std_info.catg_id) as total_students from Std_info 
inner join Catg_type on Std_info.Catg_id=Catg_type.Catg_id
INNER join Program_type on Std_info.Prog_id=Program_type.Prog_id
inner join Year_info on Std_info.year_id=Year_info.year_id
where @year=Year_info.year
group by Catg_type,Program_type.Prog_name,Year_info.year

when i excute
[spstdinfo] 2013
catg_type         Prog_name     year   total_students
Finance	          Bachelors	2013	1
Management	Bachelors	2013	1
Management	Masters	        2013	1

now when i write foreach for sp this shows error in this line
C#
foreach(var ab in result)
                  {
foreach statement cannot operate on variables of type 'int' because 'int' does not contain a public definition for 'GetEnumerator'
Posted
Updated 11-May-16 21:37pm
v3
Comments
Richard MacCutchan 12-May-16 2:45am    
The use of meaningless single character variable names, and the overuse of the var type makes your code difficult to understand. And in the long term even more difficult to maintain.

What is ProjectdbEntities and where is the definition of the spstdinfo method?
super_user 12-May-16 2:48am    
if u check varaible a at the top ProjectdbEntities a = new ProjectdbEntities(); and spstdinfo is store procedure ... i create classes through database so all properties are in ProjectdbEntities and also i call store procedure
super_user 12-May-16 2:49am    
[Inappropriate code sample removed — SA]
1) Use "Improve question" and format code;
2) The method in question is not shown anyway.
Sergey Alexandrovich Kryukov 12-May-16 2:51am    
Look at this method's definition and you will see that required parameters are different. As simple as that.
—SA
super_user 12-May-16 3:27am    
please check update question

Well, the error message is pretty obvious: foreach operates with collections, namely with objects whose classes implement the IEnumerable interface. If results is an integer variable (you didn't show its declaration in the posted code) then it doesn't and cannot be used in such a way.
Assuming results contains the number of found items, then you might iterate using a standard for loop, e.g.
C#
for (int r = 0; r < results; ++r)
{
  // do smart things here..
}
 
Share this answer
 
v2
You don;t show us where you declare result, but the error message says it's an integer - which does not implement IEnumerable (why the heck would it? It's not a collection!) so you can't use it in a foreach

Quite what you do want to use instead of result I have no idea: that code is so messy that I'm not even going to try and work out what it's trying to do! As Richard has said - you code is not written in a way that makes it easy for others (or yourself in three weeks time) to understand: use proper descriptive names, use unambiguous type declarations, and the problem will probably become obvious to you!
 
Share this answer
 
Comments
super_user 12-May-16 3:33am    
thanku for suggestion
OriginalGriff 12-May-16 4:00am    
You're welcome!
Your code does not show where result is defined and how it gets a value. At least, you informed us indirectly that result is an int. An int does not implement IEnumerable, hence a foreach is not possible. Furthermore, an int does not have a spstdinfo function. So your code won't even compile. Or did you write an extension method for int with that name?
As suggested in the comments, get rid of var and use proper type definitions. And make sure that result receives data of the expected type.
 
Share this answer
 
Comments
super_user 12-May-16 4:59am    
so now i declare var result = a.spstdinfo(year);
and now error shows
foreach statement cannot operate on variables of type 'int' because 'int' does not contain a public definition for 'GetEnumerator'

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