using CrudAppUsingADO.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CrudAppUsingADO.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
EmployeeDBContext db = new EmployeeDBContext();
List<Employee> obj = db.GetEmployees();
return View(obj);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Employee emp)
{
try
{
if (ModelState.IsValid == true)
{
EmployeeDBContext context = new EmployeeDBContext();
bool check = context.AddEmployee(emp);
if (check == true)
{
TempData["InsertMessage"] = "Data has been Inserted Successfully.";
ModelState.Clear();
return RedirectToAction("Index");
}
}
return View();
}
catch
{
return View();
}
}
public ActionResult Edit(int id)
{
EmployeeDBContext context = new EmployeeDBContext();
var row = context.GetEmployees().Find(model => model.id == id);
return View(row);
}
[HttpPost]
public ActionResult Edit(int id, Employee emp)
{
if (ModelState.IsValid == true)
{
EmployeeDBContext context = new EmployeeDBContext();
bool check = context.UpdateEmployee(emp);
if (check == true)
{
TempData["UpdateMessage"] = "Data has been Updated Successfully.";
ModelState.Clear();
return RedirectToAction("Index");
}
}
return View();
}
public ActionResult Details(int id)
{
EmployeeDBContext context = new EmployeeDBContext();
var row = context.GetEmployees().Find(model => model.id == id);
return View(row);
}
public ActionResult Delete(int id)
{
EmployeeDBContext context = new EmployeeDBContext();
var row = context.GetEmployees().Find(model => model.id == id);
return View(row);
}
[HttpPost]
public ActionResult Delete(int id, Employee emp)
{
EmployeeDBContext context = new EmployeeDBContext();
bool check = context.DeleteEmployee(id);
if (check == true)
{
TempData["DeleteMessage"] = "Data has been Deleted Successfully.";
return RedirectToAction("Index");
}
return View();
}
}
}
Employee.cs Model Class
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace CrudAppUsingADO.Models
{
public class Employee
{
public int id { get; set; }
[Required]
public string name { get; set; }
[Required]
public string gender { get; set; }
[Required]
public int age { get; set; }
[Required]
public int salary { get; set; }
[Required]
public string city { get; set; }
}
}
EmployeeDBContext.cs Model Class Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace CrudAppUsingADO.Models
{
public class EmployeeDBContext
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
public List<Employee> GetEmployees()
{
List<Employee> EmployeesList = new List<Employee>();
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("spGetEmployees", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Employee emp = new Employee();
emp.id = Convert.ToInt32(dr.GetValue(0).ToString());
emp.name = dr.GetValue(1).ToString();
emp.gender = dr.GetValue(2).ToString();
emp.age = Convert.ToInt32(dr.GetValue(3).ToString());
emp.salary = Convert.ToInt32(dr.GetValue(4).ToString());
emp.city = dr.GetValue(5).ToString();
EmployeesList.Add(emp);
}
con.Close();
return EmployeesList;
}
public bool AddEmployee(Employee emp)
{
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("spAddEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name", emp.name);
cmd.Parameters.AddWithValue("@gender", emp.gender);
cmd.Parameters.AddWithValue("@age", emp.age);
cmd.Parameters.AddWithValue("@salary", emp.salary);
cmd.Parameters.AddWithValue("@city", emp.city);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if(i > 0)
{
return true;
}
else
{
return false;
}
}
public bool UpdateEmployee(Employee emp)
{
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("spUpdateEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", emp.id);
cmd.Parameters.AddWithValue("@name", emp.name);
cmd.Parameters.AddWithValue("@gender", emp.gender);
cmd.Parameters.AddWithValue("@age", emp.age);
cmd.Parameters.AddWithValue("@salary", emp.salary);
cmd.Parameters.AddWithValue("@city", emp.city);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
return true;
}
else
{
return false;
}
}
public bool DeleteEmployee(int id)
{
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("spDeleteEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", id);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
return true;
}
else
{
return false;
}
}
}
}
No responses yet