How to create Insert, Edit, Update, Delete Data in GridView Using Asp.Net MVC using C# ?

How to create Insert, Edit, Update, Delete Data in GridView Using Asp.Net MVC using C# ?

To implement CRUD (Create, Read, Update, Delete) operations in a GridView using ASP.NET MVC with the specified table fields (EmpId, EmpFirstName, EmpLastName, Address, Sex, Email, ContactNo), follow these steps:

Step 1: Set Up Your ASP.NET MVC Project

  1. Create a New ASP.NET MVC Project:
    • Open Visual Studio.
    • Select File > New > Project.
    • Choose ASP.NET Web Application and select the MVC template.
  2. Set Up the Database:
    • Create a SQL Server database and a table named Employees with the following structure:

CREATE TABLE Employees (

EmpId INT PRIMARY KEY IDENTITY(1,1),

EmpFirstName NVARCHAR(100),

EmpLastName NVARCHAR(100),

Address NVARCHAR(255),

Sex NVARCHAR(10),

Email NVARCHAR(100),

ContactNo NVARCHAR(15)

);

Step 2: Create the Model

  1. Create a Model Class:
    • In the Models folder, create a class named Employee.cs:
    csharp

public class Employee {

public int EmpId { get; set; }

public string EmpFirstName { get; set; }

public string EmpLastName { get; set; }

public string Address { get; set; }

public string Sex { get; set; }

public string Email { get; set; }

public string ContactNo { get; set; }

}

Step 3: Create the Data Access Layer

  1. Create a Data Access Layer:
    • In the Models folder, create a class named EmployeeRepository.cs for database operations:

C# :

using System.Collections.Generic;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;

public class EmployeeRepository {

private string connectionString = ConfigurationManager.ConnectionStrings[“DefaultConnection”].ConnectionString;

public List<Employee> GetAllEmployees() {

List<Employee> employees = new List<Employee>();

using (SqlConnection con = new SqlConnection(connectionString)) {

SqlCommand cmd = new SqlCommand(“SELECT FROM Employees”, con);

con.Open();

SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read()) {

employees.Add(new Employee {

EmpId = (int)reader[“EmpId”],

EmpFirstName = reader[“EmpFirstName”].ToString(),

EmpLastName = reader[“EmpLastName”].ToString(),

Address = reader[“Address”].ToString(),

Sex = reader[“Sex”].ToString(),

Email = reader[“Email”].ToString(),

ContactNo = reader[“ContactNo”].ToString()

});

}

}

return employees;

}

public void AddEmployee(Employee employee) {

using (SqlConnection con = new SqlConnection(connectionString)) {

SqlCommand cmd = new SqlCommand(“INSERT INTO Employees (EmpFirstName, EmpLastName, Address, Sex, Email, ContactNo) VALUES (@FirstName, @LastName, @Address, @Sex, @Email, @ContactNo)”, con);

cmd.Parameters.AddWithValue(“@FirstName”, employee.EmpFirstName);

cmd.Parameters.AddWithValue(“@LastName”, employee.EmpLastName);

cmd.Parameters.AddWithValue(“@Address”, employee.Address);

cmd.Parameters.AddWithValue(“@Sex”, employee.Sex);

cmd.Parameters.AddWithValue(“@Email”, employee.Email);

cmd.Parameters.AddWithValue(“@ContactNo”, employee.ContactNo);

con.Open();

cmd.ExecuteNonQuery();

}

}

public void UpdateEmployee(Employee employee) {

using (SqlConnection con = new SqlConnection(connectionString)) {

SqlCommand cmd = new SqlCommand(“UPDATE Employees SET EmpFirstName = @FirstName, EmpLastName = @LastName, Address = @Address, Sex = @Sex, Email = @Email, ContactNo = @ContactNo WHERE EmpId = @EmpId”, con);

cmd.Parameters.AddWithValue(“@EmpId”, employee.EmpId);

cmd.Parameters.AddWithValue(“@FirstName”, employee.EmpFirstName);

cmd.Parameters.AddWithValue(“@LastName”, employee.EmpLastName);

cmd.Parameters.AddWithValue(“@Address”, employee.Address);

cmd.Parameters.AddWithValue(“@Sex”, employee.Sex);

cmd.Parameters.AddWithValue(“@Email”, employee.Email);

cmd.Parameters.AddWithValue(“@ContactNo”, employee.ContactNo);

con.Open();

cmd.ExecuteNonQuery();

}

}

public void DeleteEmployee(int empId) {

using (SqlConnection con = new SqlConnection(connectionString)) {

SqlCommand cmd = new SqlCommand(“DELETE FROM Employees WHERE EmpId = @EmpId”, con);

cmd.Parameters.AddWithValue(“@EmpId”, empId);

con.Open();

cmd.ExecuteNonQuery();

}

}

}

Step 4: Create the Controller

  1. Create a Controller:
    • Right-click the Controllers folder and add a new controller named EmployeeController.cs:
    csharp

using System.Web.Mvc;

using System.Collections.Generic;

public class EmployeeController : Controller {

private EmployeeRepository repository = new EmployeeRepository();

public ActionResult Index() {

List<Employee> employees = repository.GetAllEmployees();

return View(employees);

}

[HttpPost]

public ActionResult Create(Employee employee) {

if (ModelState.IsValid) {

repository.AddEmployee(employee);

return RedirectToAction(“Index”);

}

return View(employee);

}

[HttpPost]

public ActionResult Edit(Employee employee) {

if (ModelState.IsValid) {

repository.UpdateEmployee(employee);

return RedirectToAction(“Index”);

}

return View(employee);

}

public ActionResult Delete(int id) {

repository.DeleteEmployee(id);

return RedirectToAction(“Index”);

}

}

Step 5: Create the Views

  1. Create the Index View:
    • In the Views/Employee folder, create Index.cshtml:

@model IEnumerable<Employee>

<h2>Employees</h2>

<table class=”table”>

<thead>

<tr>

<th>First Name</th>

<th>Last Name</th>

<th>Address</th>

<th>Sex</th>

<th>Email</th>

<th>Contact No</th>

<th>Actions</th>

</tr>

</thead>

<tbody>

@foreach (var emp in Model) {

<tr>

<td>@emp.EmpFirstName</td>

<td>@emp.EmpLastName</td>

<td>@emp.Address</td>

<td>@emp.Sex</td>

<td>@emp.Email</td>

<td>@emp.ContactNo</td>

<td>

@Html.ActionLink(“Edit”, “Edit”, new { id = emp.EmpId }) |

@Html.ActionLink(“Delete”, “Delete”, new { id = emp.EmpId })

</td>

</tr>

}

</tbody>

</table>

@using (Html.BeginForm(“Create”, “Employee”, FormMethod.Post)) {

<h3>Add New Employee</h3>

<div>

@Html.LabelFor(m => m.EmpFirstName)

@Html.TextBoxFor(m => m.EmpFirstName)

</div>

<div>

@Html.LabelFor(m => m.EmpLastName)

@Html.TextBoxFor(m => m.EmpLastName)

</div>

<div>

@Html.LabelFor(m => m.Address)

@Html.TextBoxFor(m => m.Address)

</div>

<div>

@Html.LabelFor(m => m.Sex)

@Html.TextBoxFor(m => m.Sex)

</div>

<div>

@Html.LabelFor(m => m.Email)

@Html.TextBoxFor(m => m.Email)

</div>

<div>

@Html.LabelFor(m => m.ContactNo)

@Html.TextBoxFor(m => m.ContactNo)

</div>

<input type=”submit” value=”Add Employee” />

}

Step 6: Run Your Application

  • Start your application and navigate to /Employee/Index to see the CRUD operations in action.

This implementation provides a basic structure for managing employee data using ASP.NET MVC. You can further enhance the application by adding features like validation, error handling, and styling.

Leave a Comment

Your email address will not be published. Required fields are marked *