locked
Returns empty json RRS feed

  • Question

  • User229603173 posted

    I'm learning WEBAPI and this is my first sample application,

    Its a self hosted WebAPI Application, It's returning empty json data , Here is the code

    public class MarketSegments
        {
            internal string MarketSegment;
            internal int EventCount;
            internal DateTime CurrentDate;
            public MarketSegments()
            {
                MarketSegment = "None";
                EventCount = 0;
                CurrentDate = DateTime.Now;
            }
        }
    
    public List<marketsegments> GetMarketSegment()
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = MarketSegment;
                cmd.CommandType = CommandType.Text;
                cmd.Connection = sql;
                SqlDataAdapter sqlda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                sqlda.Fill(dt);
                List<marketsegments> markets = new List<marketsegments>();
                markets = (from DataRow dr in dt.Rows
                           select new MarketSegments()
                           {
                               MarketSegment = dr["MarketSegmentDesc"].ToString(),
                               EventCount = Convert.ToInt32(dr["EVentCount"].ToString()),
                               CurrentDate = Convert.ToDateTime(dr["Currentdate"].ToString())
                           }
                            ).ToList();
                return markets;
            }
    
    
    
     public class Startup
        {
            public void Configuration(IAppBuilder appbuilder)
            {
                HttpConfiguration cfg = new HttpConfiguration();
                cfg.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                    );
                appbuilder.UseWebApi(cfg);
            }
        }
    
     public class SampleController : ApiController
        {
            DBConnections cn = new DBConnections();
            // GET api/demo
            public IEnumerable<marketsegments> Get()
            {
                var ty = cn.GetMarketSegment();
                if (ty.Count > 0)
                    return ty;
                else
                {
                    ty.Add(new MarketSegments());
                    return ty;
                }
            }
    
        }
    }
    
    
    </marketsegments></marketsegments></marketsegments></marketsegments>
    Thursday, December 8, 2016 12:02 PM

Answers

  • User-2057865890 posted

    Hi Chezhian,

    You could change like this.

    DBConnections cn = new DBConnections();
    
    [HttpGet]
    [Route("api/sample/getdata")]
    public IEnumerable<MarketSegments> Get()
    {
        ...
    }
    
    public class MarketSegments
    {
       public string MarketSegment { get; set; }
       public int EventCount { get; set; }
       public DateTime CurrentDate { get; set; }
       ...
    }
    
    public List<MarketSegments> GetMarketSegment()
    {
        SqlCommand cmd = new SqlCommand();
        ...
        sqlda.Fill(dt);
        List<MarketSegments> markets = new List<MarketSegments>();
        markets = (from DataRow dr in dt.Rows
        ...
        return markets;
    }

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 9, 2016 3:10 AM

All replies

  • User-6180675 posted

    You should be returning IEnumerable<Type of object> and not just IEnumerable. I have modified your code a little bit . Let me know if it works

    public class MarketSegments
        {
            public string MarketSegment;
            public int EventCount;
            public DateTime CurrentDate;
            public MarketSegments()
            {
                MarketSegment = "None";
                EventCount = 0;
                CurrentDate = DateTime.Now;
            }
        }
    
    public List<MarketSegments> GetMarketSegment()
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = MarketSegment;
                cmd.CommandType = CommandType.Text;
                cmd.Connection = sql;
                SqlDataAdapter sqlda = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                sqlda.Fill(dt);
                // this should be a list of MarketSegments class objects 
                List<MarketSegments> markets = new List<MarketSegments>(); 
              
                markets = (from DataRow dr in dt.Rows
                           select new MarketSegments()
                           {
                               MarketSegment = dr["MarketSegmentDesc"].ToString(),
                               EventCount = Convert.ToInt32(dr["EVentCount"].ToString()),
                               CurrentDate = Convert.ToDateTime(dr["Currentdate"].ToString())
                           }
                            ).ToList();
                return markets;
            }
    
    
    
     public class Startup
        {
            public void Configuration(IAppBuilder appbuilder)
            {
                HttpConfiguration cfg = new HttpConfiguration();
                cfg.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                    );
                appbuilder.UseWebApi(cfg);
            }
        }
    
     public class SampleController : ApiController
        {
            DBConnections cn = new DBConnections();
            // GET api/demo
            // this method should return IEnumerable<MarketSegments>
            public IEnumerable<MarketSegments> Get()
            {
                var ty = cn.GetMarketSegment();
                if (ty.Count > 0)
                    return ty;  
                else
                {
                    ty.Add(new MarketSegments()); // here it will return an empty JSON response as there are no rows 
                    return ty;
                }
            }
    
        }
    }
    

    Also check what is the value of ty.Count in your controller method , so that you can check whether is it returning any rows from database.

    Thursday, December 8, 2016 12:47 PM
  • User-2057865890 posted

    Hi Chezhian,

    You could change like this.

    DBConnections cn = new DBConnections();
    
    [HttpGet]
    [Route("api/sample/getdata")]
    public IEnumerable<MarketSegments> Get()
    {
        ...
    }
    
    public class MarketSegments
    {
       public string MarketSegment { get; set; }
       public int EventCount { get; set; }
       public DateTime CurrentDate { get; set; }
       ...
    }
    
    public List<MarketSegments> GetMarketSegment()
    {
        SqlCommand cmd = new SqlCommand();
        ...
        sqlda.Fill(dt);
        List<MarketSegments> markets = new List<MarketSegments>();
        markets = (from DataRow dr in dt.Rows
        ...
        return markets;
    }

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 9, 2016 3:10 AM
  • User229603173 posted

    The problem solved after creating the properties

      public class MarketSegments
        {
            public string MarketSegment { get; set; }
            public int EventCount { get; set; }
            public  DateTime CurrentDate { get; set; }
            public MarketSegments()
            {
                MarketSegment = "None";
                EventCount = 0;
                CurrentDate = DateTime.Now;
            }
        }

    Tuesday, December 13, 2016 5:30 AM