not all code paths return a value

Jassim Al Rahma 1,616 Reputation points
2023-04-27T17:48:21.33+00:00

Hi,

I am trying to create my first API in .NET as you can see below.

But I am getting this error:

'WeatherForecastController.Get()': not all code paths return a value (CS0161) (MyTempAPI)

using System.Data;
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;

namespace MyTempAPI.Controllers;

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    List<CountryData> web;
    private SqlDataAdapter _adapter;
    private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
    {
        _logger = logger;
    }

    [HttpGet(Name = "GetCountry")]
    public IEnumerable<CountryData> Get()
    {
        try
        {
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder.DataSource = "xxxxxxxxx.database.windows.net";
            builder.UserID = "xxxxxx";
            builder.Password = "xxxxxxxxx";
            builder.InitialCatalog = "xxxxx";

            using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
            {
                // List<CountryData> data = JsonSerializer.Deserialize<List<CountryData>>(result);

                String sql = "SELECT country_code, country_name_en AS country_name FROM dbo.country";

                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    command.CommandType = System.Data.CommandType.StoredProcedure;

                    DataTable _dt = new DataTable();

                    connection.Open();

                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        web = new List<CountryData>();

                        while (reader.Read())
                        {
                            web.Add(new CountryData()
                            {
                                country_code = reader.GetString(0),
                                country_name = reader.GetString(1)
                            });
                        }
                    }
                }
            }

            return web;
        }
        catch (SqlException e)
        {
            Console.WriteLine(e.ToString());
        }
    }
}


Developer technologies | ASP.NET | ASP.NET API
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,766 Reputation points Volunteer Moderator
    2023-04-27T17:53:21.66+00:00

    the catch path in the try/catch must either throw an error (no return) or return a value.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.