How To Fetch SQL Server Data Into Dropdown in ASP.NET MVC

๐Ÿ“Œ Introduction

In real-world applications, we often need to show data in a dropdown list (like selecting Country, State, Department, etc.).

Instead of hardcoding values, we fetch them from a SQL Server database dynamically.

๐Ÿ‘‰ In this blog, you will learn:

  • What is a dropdown in MVC

  • Why do we use database data in a dropdown

  • Step-by-step implementation

  • Full working example (CRUD-ready concept)

๐Ÿค” Why Fetch Data from Database?

Imagine you are building:

  • Employee Management System โ†’ Department list

  • School System โ†’ Class list

  • E-commerce โ†’ Category list

If you hardcode values:

  • โŒ Not flexible

  • โŒ Hard to update

If you use a database

  • โœ… Dynamic

  • โœ… Easy to update

  • โœ… Real-time data

๐Ÿง  Concept Flow (Very Important)

SQL Server โ†’ Model โ†’ Controller โ†’ View (Dropdown)

๐Ÿ— Step 1: Create Database Table

CREATE TABLE Department (
    DeptId INT PRIMARY KEY IDENTITY,
    DeptName NVARCHAR(100)
);

Insert Sample Data

INSERT INTO Department (DeptName) VALUES ('HR');
INSERT INTO Department (DeptName) VALUES ('IT');
INSERT INTO Department (DeptName) VALUES ('Sales');

๐Ÿงฉ Step 2: Create Model Class

public class Department
{
    public int DeptId { get; set; }
    public string DeptName { get; set; }
}

๐Ÿ”Œ Step 3: Database Connection (ADO.NET)

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

๐ŸŽฏ Step 4: Fetch Data in Controller

public class EmployeeController : Controller
{
    string cs = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

    public ActionResult Create()
    {
        List<Department> deptList = new List<Department>();

        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM Department", con);
            con.Open();

            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                deptList.Add(new Department
                {
                    DeptId = Convert.ToInt32(dr["DeptId"]),
                    DeptName = dr["DeptName"].ToString()
                });
            }
        }

        ViewBag.Departments = deptList;

        return View();
    }
}

๐ŸŽจ Step 5: Create Dropdown in View

@{
    var deptList = ViewBag.Departments as List<YourNamespace.Models.Department>;
}

<select name="DeptId" class="form-control">
    <option value="">-- Select Department --</option>

    @foreach (var item in deptList)
    {
        <option value="@item.DeptId">@item.DeptName</option>
    }
</select>

What is this?

โšก Better Way (Using SelectList)

๐Ÿ‘‰ Cleaner and professional method

Controller

ViewBag.DepartmentList = new SelectList(deptList, "DeptId", "DeptName");

View

@Html.DropDownList("DeptId", ViewBag.DepartmentList as SelectList, "-- Select Department --", new { @class = "form-control" })

What is this?

๐Ÿ”ฅ Advanced Version (Using Stored Procedure)

SQL

CREATE PROCEDURE GetDepartments
AS
BEGIN
    SELECT * FROM Department
END

Controller

SqlCommand cmd = new SqlCommand("GetDepartments", con);
cmd.CommandType = CommandType.StoredProcedure;

๐Ÿงช Output

โœ” Dropdown will show:

  • HR

  • IT

  • Sales

โš ๏ธ Common Mistakes

  • โŒ Forgetting con.Open()

  • โŒ Wrong column name

  • โŒ Null ViewBag

  • โŒ Not casting SelectList properly

๐Ÿ’ก Pro Tips (Important ๐Ÿ”ฅ)

  • Use Entity Framework instead of ADO.NET for cleaner code

  • Use ViewModel instead of ViewBag (best practice)

  • Use AJAX for dependent dropdown (State โ†’ City)

๐ŸŽฏ Real-Life Example

When user creates employee:

Name: Abhay

Department: [IT โ–ผ]

๐Ÿ‘‰ Department list comes from database dynamically

๐Ÿ Summary

Fetching dropdown data from a SQL Server database in ASP.NET MVC enables dynamic, maintainable, and real-time user interfaces. By following a structured flow from database to view, developers can efficiently populate dropdowns, avoid hardcoding, and build scalable applications with cleaner and more professional approaches like SelectList and stored procedures.