locked
data in alert box RRS feed

  • Question

  • User639567535 posted

    i try to show result in alert box 

    i try this 

    [WebMethod]
           public static string jqufunc(int year_p)
           {
              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 += "]";
          
               //for program type
               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:[";
    
                    
                      var re = (from stdtable in a.Std_info
                                join catg in a.Catg_type on stdtable.Catg_id equals catg.Catg_id
                                join prog in a.Program_type on stdtable.Prog_id equals prog.Prog_id
                                join yea in a.Year_info on stdtable.year_id equals yea.year_id
                                where stdtable.year_id==year_p
                                group stdtable by new { catg.Catg_type1, prog.Prog_name, yea.year, stdtable.Catg_id } into z
                                select new
                                {
                                    Catg_type1 = z.Key.Catg_type1,
                                    Prog_name = z.Key.Prog_name,
                                    year = z.Key.year,
                                    Catg_id = z.Key.Catg_id
                                }).ToList();
    
                      var result = new
                      {
                          Total = re.Select(p => p.Catg_id).Distinct().Count()
                      };
    
                       res2 = res2.Substring(0, res2.Length - 1);
                       res2 += "]";
                       res2 += "},";
                     
                       }
               }
               return res + "*" + res2;
           }

     i have a dropdown in page  i filled this dropdown from db

    when i select year suppose i select 2014 year from drop down then according to the year 2014 following data is save in db

    Catg_type	Prog_name	year	total_students
    ComputerScience	Bachelors	2014	1
    Management	Bachelors	2014	1
    Finance	        Masters         2014	3
    Management	Masters	        2014	2

    now i want this in alert box 

    ['Management','ComputerScience','Finance']

    [{name:'Bachelors','2014',data:1,1,0

    name :'Masters ,type:Finance,'2014, data:2,0,3}]

     $.ajax({
    
                    type: "POST",
                    url: webmethod,
                    data: JSON.stringify({ year_p: $("#DropDownList1").val() }),
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        alert(response.d);
    
                        var data1 = response.d.split('*')[0];
                        var data2 = response.d.split('*')[1];
    
    
                        alert(data1);
                        alert(data2);

    how i do this

    Saturday, May 14, 2016 7:29 AM

Answers

  • User61956409 posted

    Hi Bakhtawar Ashiq,

    foreach (var ab in res2)
                    {
                        res2 += query.FirstOrDefault(q => q.year == yearP);
                    }
                    res2 = res2.Substring(0, res2.Length - 1);

    Please debug the code to check the value of variable “res2” before starting foreach loop. Besides, you make “res2” as source collection in foreach loop, but you modify source collection “res2” the during iterating through the collection.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 20, 2016 9:01 AM

All replies

  • User475983607 posted

    Use JSON.stringify...

    alert(JSON.stringify(response.d));

    IMHO, console.log is a much better approach.

    console.log(response);

    Open the console in the browser's developer tools (F12) to see the resulting object model.

    Saturday, May 14, 2016 1:14 PM
  • User639567535 posted

    Use JSON.stringify...

    alert(JSON.stringify(response.d));

    IMHO, console.log is a much better approach.

    console.log(response);

    Open the console in the browser's developer tools (F12) to see the resulting object model.

    i done with this but still not display

    Tuesday, May 17, 2016 7:10 AM
  • User475983607 posted

    i done with this but still not display

    You'll need to place a break point in the Web Method and step through the code to find the problem.

    Tuesday, May 17, 2016 1:16 PM
  • User639567535 posted

    now i modified my codee 

                    var query = (a.Std_info.Join(a.Catg_type, stdtable => stdtable.Catg_id,
                        catg => catg.Catg_id, (stdtable, catg) => new {stdtable, catg})
                        .Join(a.Program_type, t => t.stdtable.Prog_id, prog => prog.Prog_id, (t, prog) => new {t, prog})
                        .Join(a.Year_info, t => t.t.stdtable.year_id, yea => yea.year_id, (t, yea) => new {t, yea})
                        .Where(t=>t.t.t.stdtable.year_id==yearP)
                        .GroupBy(
                            t =>
                           new { t.t.t.catg.Catg_type1, t.t.prog.Prog_name, t.yea.year, t.t.t.stdtable.Catg_id },
                            t => t.t.t.stdtable)
                        .Select(g => new
                        {
                            g.Key.Catg_type1,
                            g.Key.Prog_name,
                            g.Key.year,
                            g.Key.Catg_id,
                            total_students = g.Select(p=>p.Catg_id).Distinct().Count()
                        })).ToList();
                    
                        
                    
                    foreach (var ab in res2)
                    {
                        res2 += query.FirstOrDefault(q => q.year == yearP);
                    }
                    res2 = res2.Substring(0, res2.Length - 1);

    i add foreach loop 

    but res2 return this  [{"name:Bachelors,data:"}{name:Masters,data:"}] 

    Wednesday, May 18, 2016 5:32 AM
  • User61956409 posted

    Hi Bakhtawar Ashiq,

    foreach (var ab in res2)
                    {
                        res2 += query.FirstOrDefault(q => q.year == yearP);
                    }
                    res2 = res2.Substring(0, res2.Length - 1);

    Please debug the code to check the value of variable “res2” before starting foreach loop. Besides, you make “res2” as source collection in foreach loop, but you modify source collection “res2” the during iterating through the collection.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 20, 2016 9:01 AM