-1

I have an error

Severity Code Description Project File Line Suppression State Error CS0161 'RateController.GetRateById(long)': not all code paths return a value shenoywebapi D:\shenoystudio\shenoywebapi\Controllers\RateController.cs 192 Active

       [Route("api/rate/getrate/{id}")]
       public Rate GetRateById(long id)
       {
           var result = new Rate();

           var dsRate = SqlHelper.ExecuteDataset(AppDatabaseConnection, CommandType.StoredProcedure, 0, "GetRateById", new SqlParameter("@Id", id));

           if (dsRate.Tables[0].Rows.Count > 0)
           {
               DataRow row = dsRate.Tables[0].Rows[0];
               result = new Rate
               {
                   Id = Convert.ToInt64(row[0]),
                   wefDate = Convert.ToDateTime(row[1]),
                   //ProductRates =new List<ProductRate>() { new ProductRate() { Rate = Convert.ToInt64(row[2])} }
               };

           }
           var ProductRatesList = new List<ProductRate>();
           var dsRateDetails = SqlHelper.ExecuteDataset(AppDatabaseConnection, CommandType.StoredProcedure, 0, "GetRateDetailsById", new SqlParameter("@RateId", id));

           if (dsRateDetails.Tables[0].Rows.Count > 0)
           {
               foreach (DataRow row in dsRateDetails.Tables[0].Rows)
               {
                   ProductRatesList.Add(new ProductRate
                   {
                       Rate = Convert.ToDecimal(row[0])
                   });
               }
               result.ProductRates = ProductRatesList;
               return result;
           }
       }

This is my rate model

{
    public class Rate
    {
        public long Id { get; set; }

        public DateTime wefDate { get; set; }

        public IList<ProductRate> ProductRates { get; set; }

    }
}

This is my ProductRate Model

namespace shenoywebapi.Models
{
    public class ProductRate
    {
        public long Id { get; set; }

        public string SerialNumber { get; set; }

        public long ProductId { get; set; }

        public string ProductName { get; set; }

        public string Unit { get; set; }

        public decimal Rate { get; set; }
    }
}

Faiz Shaikh
  • 3
  • 1
  • 6
  • GetRateById() needs to return a rate. Move `return result;` one line down, out of the `if` statement – Amélie Dupré Jul 23 '19 at 09:09
  • Possible duplicate of [c# returning error "not all code paths return a value"](https://stackoverflow.com/questions/21197410/c-sharp-returning-error-not-all-code-paths-return-a-value) – VDWWD Jul 23 '19 at 14:51

1 Answers1

0

Only on this part you have a return statement:

if (dsRateDetails.Tables[0].Rows.Count > 0)
       {
           foreach (DataRow row in dsRateDetails.Tables[0].Rows)
           {
               ProductRatesList.Add(new ProductRate
               {
                   Rate = Convert.ToDecimal(row[0])
               });
           }
           result.ProductRates = ProductRatesList;
           return result;
       }

if the code goes not to this if statement, there is no return statement

if (dsRateDetails.Tables[0].Rows.Count > 0)

you should add this before the last curly brace of the method:

return result;
Curious
  • 474
  • 1
  • 8
  • 25