bthumber
bthumber

Reputation: 7

Error in C#: Not all code path return a value

I am working in C#.

I have a connection string and I am fetching the details

class CustomerSummaries
{
    SqlConnection conn = new SqlConnection();

    public IEnumerable<T> GetAll()
    {
        try
        {
            SqlCommand cmd = new SqlCommand("GetAll", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            SqlDataReader sdr;
            conn.Open();
            sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                if (sdr.IsDBNull(sdr.GetOrdinal("ContactName")) != true)
                {
                    sdr["ContactName"].ToString();
                }
            }
        }
        catch (Exception ex)
        {
            throw;
            //lblErrorMsg.Visible = true;
            //lblErrorMsg.Text += "<br><b>getProjectLead_Error: </b> " + ex.Message;
        }
        finally
        {
            conn.Close();
        }
    } 
}

I am getting an error that says Not all code path return a value, how could fix my code?

Upvotes: 0

Views: 165

Answers (1)

ErikE
ErikE

Reputation: 50251

Your function declaration says you're going to return an IEnumerable<T>, but there is no return statement.

Furthermore, T is not a data type. I think you're going to have a bad time with that.

I also notice that sdr["ContactName"].ToString(); doesn't really do anything--it doesn't modify the column value because it's a function, not a method, and you're not assigning the result of the function to a variable to do something further with it.

Upvotes: 2

Related Questions