Click here to Skip to main content
15,742,619 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have webmethod and jquery for display chart in pop up for this .. so when i click on search button when there is data then working fine but when there is no data i want to display message "NO Data" in label .. and also pop must not display when there is no data how i do this ..

What I have tried:

check i try this code

C#
[WebMethod]
       public static string GetVo(DateTime fromdate, DateTime todate, string region)
       {

           string data2 = "[";
           try
           {
               T1 DB = new T1();
               var rea = (from vv in DB.tblVeh
                          join rv in DB.tblRen vv.MID equals rv.ID
                          join re in DB.tblRei on rv.RID equals re.RID
                          where
                          re.Region == region
                          && (vv.Name != "")
                          && re.StartDate >= fromdate
                          && re.EndDate <= todate
                          group vv by vv.Name into g
                          select new
                          {
                              Name = g.Key,
                              cnt = g.Select(t => t.Name).Count()
                          }).ToList();
     data2 += rea.ToList().Select(x => @"[""" + x.Name + @"""," + x.cnt + "]")
         .Aggregate((a, b) => a + "," + b);
               data2 += "]";

           }
           catch (Exception ex)
           {
               throw new Exception();
           }
           return data2;

       }
and jquery
JavaScript
<script type="text/javascript">
               var strarr = [["h", 9], ["t", 3], ["y", 1],["y (bunch)", 1]];
               $(function () {
                   $('[ID*=search_data]').on('click', function () {
                       var fromdate = $('[ID*=fromdate]').val();
                       var todate = $('[ID*=todate]').val();
                       var region = $('[ID*=regiondrop] option:selected')
                      [0].value;
                       var obj = {};
                       obj.fromdate = fromdate;
                       obj.todate = todate;
                       obj.region = region;
                       Getdata(obj);
                       return false;
                   });
               });
               function Getdata(obj) {
                   $.ajax({
                       type: "POST",
                       url: "WebForm1.aspx/GetVo",
                       data: JSON.stringify(obj),
                       contentType: "application/json; charset=utf-8",
                       dataType: "json",
                       async: true,
                       cache: false,
                       success: function (result) {
                           strarr = result.d;
                           var myarr = strarr;
                           Drewchart(myarr);
                       },
                       error: function (error) {
                           alert("error");
                           //$("#divcontainer").hide();
                       }
                   });
               }
               function Drewchart(result) {
                   console.log(JSON.stringify(result, null, 2));
                   $('#container').highcharts({
                       chart: {
                           type: 'pie',
                           options3d: {
                               enabled: true,
                               alpha: 45
                           }
                       },
                       title: {
                           text: 'Contents of Charts'
                       },
                       subtitle: {
                           text: '3D donut in Highcharts'
                       },
                       plotOptions: {
                           pie: {
                               innerSize: 100,
                               depth: 45
                           }
                       },
                       series: [{
                           name: 'Delivered amount',
                           data: JSON.parse(result)
                       }]

                   });
               }
    </script>

   <div id="divcontainer" style="display: none" align = "center">
         <div id="container"  class="cont_charts">

       </div>


and for pop up


JavaScript
<script type="text/javascript" >
        $(function () {
            $("#divcontainer").dialog({
                modal: true,
                autoOpen: false,
                title: "Chart",
                width: 600,
                height: 450
            });
            $("#search_data").click(function () {
                $("#divcontainer").dialog('open');

            });
        });
   </script>
Posted
Updated 20-Jul-16 22:29pm

1 solution

JavaScript
success: function (result) {
    var myData = JSON.parse(data.d);
    if (myData.length == 0)
    {
        // do something to show a message
        return;
    }
    strarr = result.d;
    var myarr = strarr;
    Drewchart(myarr); // here you could pass in myData and drop the JSON.parse in this method
},


for when there is no data in your action

C#
T1 DB = new T1();
               var rea = (from vv in DB.tblVeh
                          join rv in DB.tblRen vv.MID equals rv.ID
                          join re in DB.tblRei on rv.RID equals re.RID
                          where
                          re.Region == region
                          && (vv.Name != "")
                          && re.StartDate >= fromdate
                          && re.EndDate <= todate
                          group vv by vv.Name into g
                          select new
                          {
                              Name = g.Key,
                              cnt = g.Select(t => t.Name).Count()
                          }).ToList();
     if (rea.Any())
     {
         // rea is already a List, you don't have to keep using ToList
         data2 += rea.Select(x => @"[""" + x.Name + @"""," + x.cnt + "]")
         .Aggregate((a, b) => a + "," + b);
    }
    data2 += "]";
 
Share this answer
 
v2
Comments
super_user 21-Jul-16 5:48am    
when i try this this show this.. check please https://media.giphy.com/media/26gJznDimxRyV0zi8/giphy.gif
F-ES Sitecore 21-Jul-16 6:07am    
That's an error in your .net code and nothing to do with your original question. If you're getting exceptions then post the relevant details as text, that animated gif is of no help at all.
super_user 21-Jul-16 6:10am    
error is same "Sequence contains no elements" this means there is no data.. so how i handle this when there is no data then i want to show message no break the code
F-ES Sitecore 21-Jul-16 6:27am    
I've updated my answer
super_user 21-Jul-16 6:46am    
@F-ES Sitecore ok i try your answer.. i try this .. success: function (result) {
var myData = JSON.parse(result.d);
if (myData.length == 0 && myData!==null) {
$("#Label4").text('No data found')
return;
}
strarr = result.d;
var myarr = strarr;
Drewchart(myarr);
},
this show not any error but this also not show any message. e.g. No data found

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