๐ 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:
โ ๏ธ Common Mistakes
๐ก 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.