I have the following code that is returning an error - not all code paths return a value.

static class Products
    {
        public static List<ProductResponse> GetProducts(string productsURL)  <---- error thrown here.
        {
            //List<ProductResponse> products = new List<ProductResponse>();

            try
            {                
                var w = new WebClient();
                var jsonData = string.Empty;

                // make the select products call
                jsonData = w.DownloadString(productsURL);

                if (!string.IsNullOrEmpty(jsonData))
                {
                    // deserialize the json to c# .net
                    var response = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(jsonData);

                    if (response != null)
                    {
                        //var products = new List<ProductResponse>();

                        var products = response.response;

                        return products;
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }         
        }

        private class Product
        {
            public string product_id { get; set; }
            public string name { get; set; }
            public string selling { get; set; }
            public string buying { get; set; }
            public string default_tax_tier { get; set; }
            public string quantity_available { get; set; }
        }

        private class ProductResponse
        {
            public List<Product> Product { get; set; }
            public List<object> ProductSupplier { get; set; }
        }

        private class Messages
        {
            public string msgs { get; set; }
            public string errs { get; set; }
        }

        private class RootObject
        {
            public List<ProductResponse> response { get; set; }
            public Messages messages { get; set; }
        }
    }

What am I doing wrong that is causing the error? Any help appreciated.

Line 26 should be the last line in your method, out of the try catch block.

commented: Thanks for the quick reply. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.