Populate Drop Down List from SQL Database I am populating a drop down list from my SQL database. List should be able to be displayed with active only, inactive only or both at the same time.

OfficeRepository.cs:

    public static List&lt;OfficeRollups&gt; ListOfficeRollups(bool active, bool inactive)
    {
        List&lt;DataRow&gt; listDataRow = null;

        string whereClause = string.Empty;

        if (active &amp;&amp; !inactive)
            whereClause += @" And Active = 1";
        else if (!active &amp;&amp; inactive)
            whereClause += @" And Active = 0";


        string srtQry = @"
Select OfficeRollupID, OfficeRollupName, Active
From OfficeRollups
Where 1 = 1 " + whereClause + @"
Order By OfficeRollupName
";

        using (SqlConnection conn = new SqlConnection(Settings.ConnectionString))
        {
            using (SqlCommand objCommand = new SqlCommand(srtQry, conn))
            {
                objCommand.CommandType = CommandType.Text;
                DataTable dt = new DataTable();
                SqlDataAdapter adp = new SqlDataAdapter(objCommand);
                conn.Open();
                adp.Fill(dt);
                if (dt != null)
                {
                    listDataRow = dt.AsEnumerable().ToList();
                }
            }
        }

        var listOfficeRollups = (from o in listDataRow
                           select new OfficeRollups
                           {
                               OfficeRollupID = o.Field&lt;int&gt;("OfficeRollupID"),
                               OfficeRollupName = o.Field&lt;string&gt;("OfficeRollupName"),
                               Active = o.Field&lt;bool&gt;("Active")

                           }).ToList();

        return listOfficeRollups;
    }

    public static IEnumerable&lt;SelectListItem&gt; ListOfficeRollupsForDD(bool active, bool     inactive)
    {
        var listOfficeRollups = OfficeRepository.ListOfficeRollups(active, inactive);

        return from o in listOfficeRollups.ToList()
               where o.OfficeRollupName.Length != 0
               orderby o.OfficeRollupName
               select new SelectListItem
               {
                   Text = o.OfficeRollupName,
                   Value = o.OfficeRollupID.ToString()
               };
    }


HomeController.cs:

    public ActionResult Create()
    {
        Office currentOffice = new Office();

        var listOfficeRollups = OfficeRepository.ListOfficeRollupsForDD(true, false);
        currentOffice.OfficeRollups = listOfficeRollups;

        return View(currentOffice);
    }


Create.cshtml:

@model Office
@using (Html.BeginForm())
{
    @(Html.DropDownList("OfficeRollupID", Model.OfficeRollups))
}


Office.cs:

public class Office
{
    [Required]
    [Display(Name = "Office Rollup")]
    public IEnumerable&lt;SelectListItem&gt; OfficeRollups { get; set; }

    public Int32 OfficeRollupID { get; set; }
}
Here's a simplification of the OfficeRepository methods. Note the other IDisposable types in using blocks:

    public static IEnumerable&lt;OfficeRollups&gt; ListOfficeRollups(bool active, bool inactive)
    {
        var whereClause = active &amp;&amp; !inactive
            ? @"Where Active = 1"
            : (!active &amp;&amp; inactive ? @"Where Active = 0" : string.Empty);
        var srtQry = @"
Select OfficeRollupID, OfficeRollupName, Active
From OfficeRollups
" + whereClause + @"
Order By OfficeRollupName
";

        using (var conn = new SqlConnection(Settings.ConnectionString))
        using (var objCommand = new SqlCommand(srtQry, conn) { CommandType = CommandType.Text })
        using (var dt = new DataTable())
        using (var adp = new SqlDataAdapter(objCommand))
        {
            conn.Open();
            adp.Fill(dt);
            return dt.AsEnumerable().Select(o =&gt; new OfficeRollups
            {
                OfficeRollupID = o.Field&lt;int&gt;("OfficeRollupID"),
                OfficeRollupName = o.Field&lt;string&gt;("OfficeRollupName"),
                Active = o.Field&lt;bool&gt;("Active")
            }).ToList();
        }
    }

    public static IEnumerable&lt;SelectListItem&gt; ListOfficeRollupsForDD(bool active, bool inactive)
    {
        var listOfficeRollups = OfficeRepository.ListOfficeRollups(active, inactive);

        return listOfficeRollups
                .Where(o =&gt; !string.IsNullOrEmpty(o.OfficeRollupName))
                .OrderBy(o =&gt; o.OfficeRollupName)
                .Select(o =&gt; new SelectListItem
                {
                    Text = o.OfficeRollupName,
                    Value = o.OfficeRollupID.ToString()
                })
                .ToList();
    }