๐Ÿ“Œ Introduction

In real-world web applications, we need to:

  • Insert data

  • Display data

  • Update data

  • Delete data

This is called CRUD operations.

In this article, we will build a Student Management System using:

  • ASP.NET MVC

  • ADO.NET

  • SQL Server

Everything is explained in simple words for beginners.

๐Ÿงฑ STEP 1: Create Database

Open SQL Server and run:

Code
CREATE DATABASE StudentMVCDB;

USE StudentMVCDB;

CREATE TABLE Students
(
    Id INT PRIMARY KEY IDENTITY(1,1),
    Name NVARCHAR(100),
    Email NVARCHAR(100),
    City NVARCHAR(100)
);

โœ… Explanation

  • IDENTITY(1,1) โ†’ Auto-increment ID

  • NVARCHAR(100) โ†’ Stores text data

  • PRIMARY KEY โ†’ Unique identifier

๐Ÿ–ฅ STEP 2: Create MVC Project

  1. Open Visual Studio

  2. Create new ASP.NET Web Application

  3. Choose MVC Template

๐Ÿ”— STEP 3: Add Connection String

Open Web.config

Code
<connectionStrings>
  <add name="dbcs"
       connectionString="Data Source=YOUR_SERVER;Initial Catalog=StudentMVCDB;Integrated Security=True"
       providerName="System.Data.SqlClient"/>
</connectionStrings>

โœ… Explanation

  • Data Source โ†’ SQL Server name

  • Initial Catalog โ†’ Database name

  • Integrated Security=True โ†’ Windows authentication

๐Ÿ“ STEP 4: Create Model

Right click Models โ†’ Add Class โ†’ Student.cs

Code
public class Student
{
    public int Id { get; set; }      
    public string Name { get; set; } 
    public string Email { get; set; }
    public string City { get; set; }
}

โœ… Explanation

This class represents the Students table.

  • Id โ†’ Primary key

  • Name, Email, City โ†’ Columns of table

Model is used to transfer data between Controller and View.

๐ŸŽฎ STEP 5: Create Controller

Right click Controllers โ†’ Add โ†’ MVC Empty Controller

Name: StudentController

Add namespaces:

Code
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;
using YourProjectName.Models;

๐Ÿ“Œ READ (Display Data)

Code

Code
public ActionResult Index()
{
    string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;

    List<Student> students = new List<Student>();

    using (SqlConnection con = new SqlConnection(cs))
    {
        string query = "SELECT * FROM Students";
        SqlCommand cmd = new SqlCommand(query, con);

        con.Open();

        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            Student student = new Student();

            student.Id = Convert.ToInt32(dr["Id"]);
            student.Name = dr["Name"].ToString();
            student.Email = dr["Email"].ToString();
            student.City = dr["City"].ToString();

            students.Add(student);
        }
    }

    return View(students);
}

๐Ÿ”Ž Line-by-Line Explanation

  • ConfigurationManager... โ†’ Gets connection string

  • List<Student> โ†’ Stores multiple students

  • SqlConnection โ†’ Connects to SQL Server

  • SqlCommand โ†’ Executes SQL query

  • ExecuteReader() โ†’ Reads data

  • while(dr.Read()) โ†’ Reads row one by one

  • students.Add() โ†’ Adds data to list

  • return View(students) โ†’ Sends data to View

๐Ÿ–ผ Index View (Display Data)

Code
@model IEnumerable<YourProjectName.Models.Student>

<h2>Student List</h2>

<p>@Html.ActionLink("Add New Student", "Create")</p>

<table border="1">
<tr>
    <th>Id</th>
    <th>Name</th>
    <th>Email</th>
    <th>City</th>
    <th>Action</th>
</tr>

@foreach (var item in Model)
{
<tr>
    <td>@item.Id</td>
    <td>@item.Name</td>
    <td>@item.Email</td>
    <td>@item.City</td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
        @Html.ActionLink("Delete", "Delete", new { id = item.Id })
    </td>
</tr>
}
</table>

โœ… Explanation

  • @model IEnumerable<Student> โ†’ Accepts list of students

  • foreach โ†’ Loops through list

  • ActionLink โ†’ Creates Edit & Delete links

๐Ÿ“Œ CREATE (Insert Data)

GET Method

Code
public ActionResult Create()
{
    return View();
}

Explanation

Shows empty form.

POST Method

Code
[HttpPost]
public ActionResult Create(Student student)
{
    string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;

    using (SqlConnection con = new SqlConnection(cs))
    {
        string query = "INSERT INTO Students(Name,Email,City) VALUES(@Name,@Email,@City)";
        SqlCommand cmd = new SqlCommand(query, con);

        cmd.Parameters.AddWithValue("@Name", student.Name);
        cmd.Parameters.AddWithValue("@Email", student.Email);
        cmd.Parameters.AddWithValue("@City", student.City);

        con.Open();
        cmd.ExecuteNonQuery();
    }

    return RedirectToAction("Index");
}

Explanation

  • @Name โ†’ SQL parameter

  • AddWithValue() โ†’ Sends value safely

  • ExecuteNonQuery() โ†’ Executes Insert

  • RedirectToAction() โ†’ Goes back to list page


๐Ÿ“Œ UPDATE

GET

Code
public ActionResult Edit(int id)
{
    string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
    Student student = new Student();

    using (SqlConnection con = new SqlConnection(cs))
    {
        string query = "SELECT * FROM Students WHERE Id=@Id";
        SqlCommand cmd = new SqlCommand(query, con);
        cmd.Parameters.AddWithValue("@Id", id);

        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            student.Id = Convert.ToInt32(dr["Id"]);
            student.Name = dr["Name"].ToString();
            student.Email = dr["Email"].ToString();
            student.City = dr["City"].ToString();
        }
    }

    return View(student);
}

POST

Code
[HttpPost]
public ActionResult Edit(Student student)
{
    string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;

    using (SqlConnection con = new SqlConnection(cs))
    {
        string query = "UPDATE Students SET Name=@Name, Email=@Email, City=@City WHERE Id=@Id";
        SqlCommand cmd = new SqlCommand(query, con);

        cmd.Parameters.AddWithValue("@Id", student.Id);
        cmd.Parameters.AddWithValue("@Name", student.Name);
        cmd.Parameters.AddWithValue("@Email", student.Email);
        cmd.Parameters.AddWithValue("@City", student.City);

        con.Open();
        cmd.ExecuteNonQuery();
    }

    return RedirectToAction("Index");
}

๐Ÿ“Œ DELETE

Code
public ActionResult Delete(int id)
{
    string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;

    using (SqlConnection con = new SqlConnection(cs))
    {
        string query = "DELETE FROM Students WHERE Id=@Id";
        SqlCommand cmd = new SqlCommand(query, con);

        cmd.Parameters.AddWithValue("@Id", id);

        con.Open();
        cmd.ExecuteNonQuery();
    }

    return RedirectToAction("Index");
}

๐Ÿ” Why We Use Parameters?

We use:

Code
cmd.Parameters.AddWithValue()

Because:

  • Prevents SQL Injection

  • Improves security

  • Safe coding practice

๐ŸŽ‰ Final Output

You will have:

โœ” Student list page

โœ” Add student form

โœ” Edit student

โœ” Delete student

Complete working MVC CRUD Application.