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.