0% found this document useful (0 votes)
11 views

C#

This document contains stored procedures to manage employee data in a database. It includes procedures for adding, getting, updating, and deleting employee records.

Uploaded by

Neel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

C#

This document contains stored procedures to manage employee data in a database. It includes procedures for adding, getting, updating, and deleting employee records.

Uploaded by

Neel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

insert......

Create procedure [dbo].[AddNewEmpDetails]


(
@Name varchar (50),
@City varchar (50),
@Address varchar (50)
)
as
begin
Insert into Employee values(@Name,@City,@Address)
End

view........

Create Procedure [dbo].[GetEmployees]


as
begin
select *from Employee
End

update.........

Create procedure [dbo].[UpdateEmpDetails]


(
@EmpId int,
@Name varchar (50),
@City varchar (50),
@Address varchar (50)
)
as
begin
Update Employee
set Name=@Name,
City=@City,
Address=@Address
where Id=@EmpId
End

Delete..........

Create procedure [dbo].[DeleteEmpById]


(
@EmpId int
)
as
begin
Delete from Employee where Id=@EmpId
End
models............................

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using MVCADO.Models;
public class EmpRepository
{

private SqlConnection con;


//To Handle connection related activities
private void connection()
{
string constr =
ConfigurationManager.ConnectionStrings["getconn"].ToString();
con = new SqlConnection(constr);

}
//To Add Employee details
public bool AddEmployee(EmpModel obj)
{

connection();
SqlCommand com = new SqlCommand("AddNewEmpDetails", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@Name", obj.Name);
com.Parameters.AddWithValue("@City", obj.City);
com.Parameters.AddWithValue("@Address", obj.Address);

con.Open();
int i = com.ExecuteNonQuery();
con.Close();
if (i >= 1)
{

return true;

}
else
{

return false;
}

}
//To view employee details with generic list
public List<EmpModel> GetAllEmployees()
{
connection();
List<EmpModel> EmpList =new List<EmpModel>();
SqlCommand com = new SqlCommand("GetEmployees", con);
com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable dt = new DataTable();

con.Open();
da.Fill(dt);
con.Close();
//Bind EmpModel generic list using dataRow
foreach (DataRow dr in dt.Rows)
{

EmpList.Add(

new EmpModel {

Empid = Convert.ToInt32(dr["Id"]),
Name =Convert.ToString( dr["Name"]),
City = Convert.ToString( dr["City"]),
Address = Convert.ToString(dr["Address"])

}
);
}

return EmpList;
}
//To Update Employee details
public bool UpdateEmployee(EmpModel obj)
{

connection();
SqlCommand com = new SqlCommand("UpdateEmpDetails", con);

com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@EmpId", obj.Empid);
com.Parameters.AddWithValue("@Name", obj.Name);
com.Parameters.AddWithValue("@City", obj.City);
com.Parameters.AddWithValue("@Address", obj.Address);
con.Open();
int i = com.ExecuteNonQuery();
con.Close();
if (i >= 1)
{

return true;
}
else
{
return false;
}
}
//To delete Employee details
public bool DeleteEmployee(int Id)
{

connection();
SqlCommand com = new SqlCommand("DeleteEmpById", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@EmpId", Id);

con.Open();
int i = com.ExecuteNonQuery();
con.Close();
if (i >= 1)
{
return true;
}
else
{

return false;
}
}
}

controller....................

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCADO.Models;

namespace MVCADO.Controllers
{

public class EmployeeController : Controller


{

// GET: Employee/GetAllEmpDetails
public ActionResult GetAllEmpDetails()
{

EmpRepository EmpRepo = new EmpRepository();


ModelState.Clear();
return View(EmpRepo.GetAllEmployees());
}
// GET: Employee/AddEmployee
public ActionResult AddEmployee()
{
return View();
}

// POST: Employee/AddEmployee
[HttpPost]
public ActionResult AddEmployee(EmpModel Emp)
{
try
{
if (ModelState.IsValid)
{
EmpRepository EmpRepo = new EmpRepository();
if (EmpRepo.AddEmployee(Emp))
{
ViewBag.Message = "Employee details added successfully";
}
}

return View();
}
catch
{
return View();
}
}

// GET: Employee/EditEmpDetails/5
public ActionResult EditEmpDetails(int id)
{
EmpRepository EmpRepo = new EmpRepository();

return View(EmpRepo.GetAllEmployees().Find(Emp => Emp.Empid == id));

// POST: Employee/EditEmpDetails/5
[HttpPost]

public ActionResult EditEmpDetails(int id,EmpModel obj)


{
try
{
EmpRepository EmpRepo = new EmpRepository();

EmpRepo.UpdateEmployee(obj);
return RedirectToAction("GetAllEmpDetails");
}
catch
{
return View();
}
}

// GET: Employee/DeleteEmp/5
public ActionResult DeleteEmp(int id)
{
try
{
EmpRepository EmpRepo = new EmpRepository();
if (EmpRepo.DeleteEmployee(id))
{
ViewBag.AlertMsg = "Employee details deleted successfully";

}
return RedirectToAction("GetAllEmpDetails");

}
catch
{
return View();
}
}
}
}

You might also like